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.
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.
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 ) );
#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;
}
}
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();
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.
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 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;
#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.
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
}
elseif (integer > nextLargestInteger) {
// do something else
}
//.... continue on for the rest of the cases
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;
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.