Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Dyndrilliac on February 13, 2004, 08:44 AM

Title: Problem with Char variables.
Post by: Dyndrilliac on February 13, 2004, 08:44 AM
#include <stdio.h>
#include <iostream.h>
#include <conio.h>

void main()
{                                

   int t;
   int x;
   int y;
   int z = 1;

   char QA1;
   char QA2;

   while (z == 1)
   {
      printf("Welcome to the amazing computer repair wizard!\n\n");
      
      printf("  1)  Does the computer beep on startup? (Y/N)   ");
      cin>>QA1;

      if ((QA1)==('Y'))
      {
         y = 1;
      }
      else
      {
         y = 0;
      }

      printf("  2)  Does the hardrive spin? (Y/N)   ");
      cin>>QA2;

      if ((QA2)==('Y'))
      {
         x = 2;
      }
      else
      {
         x = 0;
      }

      t = (x+y);

      switch (t)
      {
      case 0:
         printf("\nCheck the speaker contacts.\n\n\n");
         break;
      case 1:
         printf("\nCheck the drive contacts.\n\n\n");
         break;
      case 2:
         printf("\nUnknown problem.\n\n\n");
         break;
      case 3:
         printf("\nContact tech support.\n\n\n");
         break;
      default:
         printf("\nError! The program has performed an illegal operation, unknown exception!\n\n\n");
      }
   }
}


For some reason, I always get the message "Check speaker contacts", so I'm assuming there is a problem with my characters or switch statement
Title: Re:Problem with Char variables.
Post by: iago on February 13, 2004, 12:09 PM
Honestly, that's a terrible way to do things.  First of all, use good variable names; secondly, use constants.  Look at the difference between this:
int main()
{
int x = 43;
while(x >= 0)
 if(y()) { x--; }

 return 0;
}

and this:

#define TOTAL_COMPUTERS 43
int main()
{
 int remainingComputers = TOTAL_COMPUTERS;

 while(remainingComputers > 0)
   if(oneIsBroken) { remainingComputers--; }

 return 0;
}


Admittedly, the second one took longer to write, but which one makes more sense?

I would strongly recommend you learn to code using constants and good variable names.

<Edit> What the hell is the forum doing with my code tags??
Title: Re:Problem with Char variables.
Post by: Eli_1 on February 13, 2004, 01:33 PM
I'm very new to C/C++ programming so correct me if I'm wrong, but shouldn't you use brackets with your cases?

e.g.

switch(t) {
     case 0: {
        printf("\nCheck the speaker contacts.\n\n\n");
        break;
     }
     case 1: {
        printf("\nCheck the drive contacts.\n\n\n");
        break;
     }
     case 2: {
     }
}

ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?
Title: Re:Problem with Char variables.
Post by: UserLoser. on February 13, 2004, 01:38 PM
Quote from: Eli_1 on February 13, 2004, 01:33 PM
I'm very new to C/C++ programming so correct me if I'm wrong, but shouldn't you use brackets with your cases?

e.g.

switch(t) {
     case 0: {
        printf("\nCheck the speaker contacts.\n\n\n");
        break;
     }
     case 1: {
        printf("\nCheck the drive contacts.\n\n\n");
        break;
     }
     case 2: {
     }
}

ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?


Brackets are not required for cases, and declaring a variable like QA1[5] creates an array, otherwise QA1 would only be able to hold one character if the [5] wasn't there
Title: Re:Problem with Char variables.
Post by: Dyndrilliac on February 13, 2004, 01:40 PM
Quote from: Eli_1 on February 13, 2004, 01:33 PM
I'm very new to C/C++ programming so correct me if I'm wrong, but shouldn't you use brackets with your cases?

e.g.

switch(t) {
     case 0: {
        printf("\nCheck the speaker contacts.\n\n\n");
        break;
     }
     case 1: {
        printf("\nCheck the drive contacts.\n\n\n");
        break;
     }
     case 2: {
     }
}

ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?


No, and char Variable[5] means you have 5 spaces for that character, similar to a string of 5 characters - everything after is truncated.

Responding to iago, I don't mind how poorly done it looks, as long as it works; My way maybe poor style but I wan't it to work before I go making it look pretty.
Title: Re:Problem with Char variables.
Post by: Eli_1 on February 13, 2004, 01:40 PM
oh ok, thanks

what about using strcmp()?
Title: Re:Problem with Char variables.
Post by: Dyndrilliac on February 13, 2004, 01:45 PM
Quote from: Eli_1 on February 13, 2004, 01:40 PM
oh ok, thanks

what about using strcmp()?

I'm just making a small app for class; It doesn't have to be fancy, thus why I used very simple methods to achieve my effect. Btw, I fixed my problem.
Title: Re:Problem with Char variables.
Post by: Eli_1 on February 13, 2004, 01:47 PM
haha, ok  ;D