• Welcome to Valhalla Legends Archive.
 

Problem with Char variables.

Started by Dyndrilliac, February 13, 2004, 08:44 AM

Previous topic - Next topic

Dyndrilliac

#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
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

iago

#1
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??
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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.

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]; ?

UserLoser.

#3
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

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Eli_1

#5
oh ok, thanks

what about using strcmp()?

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.