Help with char and if statements!

Pages: 12
I have to write a program that ask the user to type in a character which will be stored(this character has no use). After that the user is asked to type in the letter "A" for an alien to pop up. Once the alien pops up, the user is asked to "Press Q to quit."
I got the first half to work but am having troubles with the second part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
int main()
{
          char a;
          char q;
          char x;
        
        printf("Please enter a letter.\n");
        x = getchar();
        printf("You entered %c!", x); 
         
        printf("\nPress A to show an alien!\n %c ", a);
                      
        a = getchar();
        if(a == 'A' || a == 'a')
             {
                printf("Alien.");
                
                //Alien will be displayed here
                
                getch();
                return 0;
                
                }
        q = getchar();
        if(q == 'Q' || q == 'q')
             {
                printf("Quiting...");
                
                getch();
                return 0;
                }
}


When I compile and run the program, the first part works, but it skips over the if statements.


Please enter a letter.
k
You entered k!
Press A to show an alien!
 v


I also have no idea where the v is coming from?

Thanks
The problem is in thsi statement

printf("\nPress A to show an alien!\n %c ", a);

I think, you were not going to output undefined value of uninitialized variable 'a'.
So instead of this statement write

printf("\nPress A to show an alien!\n" );

Or use puts instead of printf. For example

puts("\nPress A to show an alien!" );
Last edited on
I changed what you recommended, and the program does work better now but when I type in either "a" or "A", my printf("Alien.") does not display. and also the if statement regarding pressing q to quit is skipped.

Any ideas??
Try the following:
1) add header <ctype.h>
2) all statements with getchar substitute for


do { q = fgetc( stdin ); } while ( !isspace( q ) );

where variables x, a shall be used together with q, that is there will be three statements with different variables

do { x = fgetc( stdin ); } while ( !isspace( x ) );
do { a = fgetc( stdin ); } while ( !isspace( a ) );
do { q = fgetc( stdin ); } while ( !isspace( q ) );



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <ctype.h>

int main()
{
          char a;
          char q;
          char x;
        
        printf("Please enter a letter.\n");
        do { x = fgetc( stdin ); } while ( !isspace( x ) );
        printf("You entered %c!", x); 
         
        printf("\nPress A to show an alien!\n %c ", a);
                      
        do { a = fgetc( stdin ); } while ( !isspace( a ) );
        if(a == 'A' || a == 'a')
             {
                printf("Alien.");
                
                //Alien will be displayed here
                
                getch();
                return 0;
                
                }
        do { q = fgetc( stdin ); } while ( !isspace( q ) );
        if(q == 'Q' || q == 'q')
             {
                printf("Quiting...");
                
                getch();
                return 0;
                }
} 
Last edited on
If I remember correctly, getchar() only returns one character at a time but when you entered the first letter you put k<newline> into the input buffer so when you call getchar() the second time, it reads the newline character into the variable a. Simplest solution is to put:

1
2
x = getchar();
getchar();  //remove newline from input buffer 


And you'll proabably have to do the same thing to prevent the problem at q=getchar();
Last edited on
ssrun, thank you!

that fixed most of the problem but now im having issues with the last if statement. It will come up with Please press Q to quit but it doesn't carry out the if statement when i do enter in a Q.

Here is my new code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
int main()
{
          char a;
          char q;
          char x;
        
        printf("Please enter a letter.\n");
        x = getchar();
        getchar();
        printf("You entered %c!", x); 
        
        sleep(1000);
        
        printf("\nPress A to show an alien!\n");
                      
        a = getchar();
        getchar();
        if(a == 'A' || a == 'a')
             {
                printf("Alien.\n");
                
                //Alien will be displayed here
             
                }
        sleep(1000);
        printf("Please press Q to quit.");
        q = getchar();
        getchar();
        if(q == 'Q' || q == 'q')
             {
                printf("Quiting...");
                
                sleep(1000);
                
            
                }
                  

return 0;              
}


Also, not to be an annoyance, but how can i get it to only close when Q is pressed and no other key?
Last edited on
I showed already the code that does your task. as for your code I think that the problem is that you forgot insert getchar after statement

a = getchar();

It ilooks not very well when two getchar follow each other. it is better to use the code I showed. Its semantic is more clear.
Last edited on
For some reason the getchar(); did not show up in the code above, it still does the same thing regardless if it is in there or not.

I tried your code that you had posted but it gave me the same errors?
So far we have only gone over char, printf, scanf, and the basic commands, nothing to complicated yet like you have posted.

I'm not sure about the if statement after entering Q since the code looks correct. As for only exiting when the user presses Q you will need to enclose your program inside a loop. The simplest one to use based on the code you have is a do while loop.
@GeneralBamboo
I tried your code that you had posted but it gave me the same errors?
So far we have only gone over char, printf, scanf, and the basic commands, nothing to complicated yet like you have posted.


I did not know how did you try my code but it works correctly.
I am currently tring the do while loop for closing.

And for the press Q to quit, if I do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
int main()
{
          char a;
          char q;
          char x;
        
        printf("Please enter a letter.\n");
        x = getchar();
        getchar();
        printf("You entered %c!", x); 
        
        sleep(1000);
        
        printf("\nPress A to show an alien!\n");
                      
        a = getchar();
        getchar();
        if(a == 'A' || a == 'a')
             {
                printf("Alien.\n");
                
                //Alien will be displayed here
             
                }
        sleep(1000);
        printf("Please press Q to quit.");
        q = getchar();
        getchar();
        if(q == 'Q' || q == 'q')
             {
                printf("Quiting...");
                
                sleep(1000);
                
                }
               printf("Exiting");
                  

return 0;              
}


what ever i put outside the } of the quiting if statement (such as printf("Exiting"); above) is executed then the program closes.
Last edited on
I have to write a program for my computer science class and everytime I try to run this program I keep getting one digit and then eleven digits as my output. If you could help I would so greatly appreciate it!!!

// How many digits
#include <iostream>
using namespace std;

int main ()
{int integer;
cout << "Enter an integer." ;
cin >> integer;
cout << "The number of digits the integer has is " << endl;
if (integer < 0){
integer = integer * -1;
cout << "negative. " << endl;
}
else if (integer > 0){
cout << "one digit. " << endl;
}
else if (integer > 9){
cout << "two digits. " << endl;
}
else if (integer > 99){
cout << "three digits. " << endl;
}
else if (integer > 999){
cout << "four digits. " << endl;
}
else if (integer > 9999){
cout << "five digits. " << endl;
}
else if (integer > 99999){
cout << "six digits. " << endl;
}
else if (integer > 999999){
cout << "seven digits. " << endl;
}
else if (integer > 9999999){
cout << "eight digits. " << endl;
}
else if (integer > 99999999){
cout << "nine digits. " << endl;
}
else if (integer > 999999999){
cout << "ten digits. " << endl;
}
else (integer > 9999999999LLU);
cout << "eleven digits. " << endl;

system ("pause");
return 0;
}


Thank you!!!
GeneralBamboo try using this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
int main()
{
          char a;
          char q;
          char x;
        
do {
        printf("Please enter a letter.\n");
        x = getchar();
        getchar();
        printf("You entered %c!", x); 
        
        sleep(1000);
        
        printf("\nPress A to show an alien!\n");
                      
        a = getchar();
        getchar();
        if(a == 'A' || a == 'a')
             {
                printf("Alien.\n");
                
                //Alien will be displayed here
             
                }
        sleep(1000);
        printf("Please press Q to quit.");
        q = getchar();
        getchar();
} while (q != 'Q' && q != 'q') ;
 
               printf("Quiting...");                
               printf("Exiting");
                  

return 0;              
}


As for the printing of "Quitting" it seems to work fine for me when I copied and compiled the code.
Last edited on
@sbgreen

The reason is that you need to reverse the order of your inequality checks. Any positive integer will always evaluate integer > 0 as true so your other else if statements won't run. The way to check it is to check the most narrow case and then expand the case as you go down the else if chain.

In other words:

1
2
3
4
5
6
7
if (integer > largestInteger) {
  //do something
}
else if (integer >  nextLargestInteger) {
 // do something else
}
//.... continue on for the rest of the cases 


Hope that helps.
Last edited on
Thanks! Ok, I did that but now when I type in a ten digit or eleven digit number, the output is saying it is ten digits and then the next line says eleven digits for both ten and eleven. What is wrong?


// How many digits
#include <iostream>
using namespace std;

int main ()
{int integer;
cout << "Enter an integer." ;
cin >> integer;
cout << "The number of digits the integer has is " << endl;
if (integer < 0){
integer = integer * -1;
cout << "negative. " << endl;
}
else if (integer <= 9){
cout << "one digit. " << endl;
}
else if (integer <= 99){
cout << "two digits. " << endl;
}
else if (integer <= 999){
cout << "three digits. " << endl;
}
else if (integer <= 9999){
cout << "four digits. " << endl;
}
else if (integer <= 99999){
cout << "five digits. " << endl;
}
else if (integer <= 999999){
cout << "six digits. " << endl;
}
else if (integer <= 9999999){
cout << "seven digits. " << endl;
}
else if (integer <= 99999999){
cout << "eight digits. " << endl;
}
else if (integer <= 999999999){
cout << "nine digits. " << endl;
}
else if (integer <= 9999999999LLU){
cout << "ten digits. " << endl;
}
else (integer <= 99999999999LLU);
cout << "eleven digits. " << endl;

system ("pause");
return 0;
}


I hope you can help!!
Last edited on
The last else statement should be an else if check and enclose the cout << "eleven digits" << endl in braces. Right now the cout will always print since it's not in a conditional statement.

The use of else generally doesn't involve a condition as it's a last resort kind of check.
Unfortunately it still outputs the same thing.

// How many digits
#include <iostream>
using namespace std;

int main ()
{int integer;
cout << "Enter an integer." ;
cin >> integer;
cout << "The number of digits the integer has is " << endl;
if (integer < 0){
integer = integer * -1;
cout << "negative. " << endl;
}
else if (integer <= 9){
cout << "one digit. " << endl;
}
else if (integer <= 99){
cout << "two digits. " << endl;
}
else if (integer <= 999){
cout << "three digits. " << endl;
}
else if (integer <= 9999){
cout << "four digits. " << endl;
}
else if (integer <= 99999){
cout << "five digits. " << endl;
}
else if (integer <= 999999){
cout << "six digits. " << endl;
}
else if (integer <= 9999999){
cout << "seven digits. " << endl;
}
else if (integer <= 99999999){
cout << "eight digits. " << endl;
}
else if (integer <= 999999999){
cout << "nine digits. " << endl;
}
else if (integer <= 9999999999LLU){
cout << "ten digits. " << endl;
}
else if (integer <= 99999999999LLU);
cout << "eleven digits. " << endl;

system ("pause");
return 0;
}
The last else if should be:

1
2
3
4
5
6
else if (integer <= 99999999999LLU) {
  cout << "elevent digits." << endl;
}

system("pause");
return 0;
It says there is an expected primary expression before "[" token? This is what I put in:

else if (integer <= 9999999999LLU){
cout << "ten digits. " << endl;
}
1
2
3
4
5
else if (integer <= 99999999999LLU);
cout << "eleven digits. " << endl;

system ("pause");
return 0;

}
else if (integer <= 9999999999LLU);
cout << "ten digits. " << endl;
}
else if (integer <= 99999999999LLU){
cout << "eleven digits. " << endl;

system ("pause");
return 0;
}


I did that, and then an error message came up saying expected unqualified id before else
Pages: 12