Valhalla Legends Forums Archive | C/C++ Programming | Problem with Char variables.

AuthorMessageTime
Dyndrilliac
[code]#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");
      }
   }
}[/code]

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
February 13, 2004, 2:44 PM
iago
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:
[code]int main()
{
int x = 43;
while(x >= 0)
if(y()) { x--; }

return 0;
}[/code]
and this:
[code]
#define TOTAL_COMPUTERS 43
int main()
{
int remainingComputers = TOTAL_COMPUTERS;

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

return 0;
}[/code]

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??
February 13, 2004, 6:09 PM
Eli_1
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.
[code]
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: {
}
}
[/code]
ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?
February 13, 2004, 7:33 PM
UserLoser.
[quote author=Eli_1 link=board=30;threadid=5268;start=0#msg44062 date=1076700829]
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.
[code]
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: {
}
}
[/code]
ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?

[/quote]

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
February 13, 2004, 7:38 PM
Dyndrilliac
[quote author=Eli_1 link=board=30;threadid=5268;start=0#msg44062 date=1076700829]
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.
[code]
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: {
}
}
[/code]
ect...?
Also, I thought you had to declare chars like so
char QA1[5]; ?

[/quote]

No, and [code]char Variable[5][/code] 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.
February 13, 2004, 7:40 PM
Eli_1
oh ok, thanks

what about using strcmp()?
February 13, 2004, 7:40 PM
Dyndrilliac
[quote author=Eli_1 link=board=30;threadid=5268;start=0#msg44067 date=1076701255]
oh ok, thanks

what about using strcmp()?
[/quote]

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.
February 13, 2004, 7:45 PM
Eli_1
haha, ok ;D
February 13, 2004, 7:47 PM

Search