# how to classify a variable?

## Recommended Posts

NIAB    122
im messing around with some code, and i forgot how to classify a variable, if thats how you'd say it. heres what i have .. #include<iostream> using namespace std; int main() { cout<<"please type 'A'"<<input<<endl; if input==a { cout<<"good job! you typed 'A'"<<endl; } else { cout<<"you failed typing 'A'"<<endl; } } if says "input" was'nt declared. help?

##### Share on other sites
blakedev    133
You can do that in BASIC, but not in C++.
It says that input isn't decleared. Since you're just typing in an A, you'd need a char. So before the first cout statement add:

char input;

Also, you need to enclose input == 'a' in parenthenses, and add 'return 0;' before the last curly bracket.

##### Share on other sites
ToohrVyk    1595
char input; // define inputcout << "Please input character 'A'\n";if (cin >> input)   if (input == 'A') cout << "Cool!\n";  else cout << "You failed!\n";else cout << "I cannot read your input!\n";

##### Share on other sites
jyk    2094
Do you mean 'declare' a variable? In C/C++:
type name;// Example:int myVariable;
Also, std::cout is an output stream, not an input stream. To have the user input a character (or whatever), you'll need to use std::cin.

##### Share on other sites
NIAB    122
here is my fixed code, but the program opens, then closes right
afterward .. what can i do to stop it?

#include<iostream>
using namespace std;

int main()
{
char input;
if (input== 'a')
{
cout<<"good job! you typed 'A' \n"<<endl;
}
else
{
cout<<"you failed typing 'A' \n"<<endl;
}
return 0;
}

##### Share on other sites
Zaris    183
you need to use cin for input. You can't combine it in the cout statement.

cin>>input;

##### Share on other sites
NIAB    122
hurray! it works! and whats its purpose?! nothing :D
just a test, and i now know ever thing i coded in
that program. well, except what the namespace std is,
and the iostream thing.

thanks for the help people.

##### Share on other sites
nooblet    167
Here is a better way to do it in C++

#include <iostream>#include <string>using namespace std;int main(){  string input;  cout << "Please type the letter A: "; cin >> input;  if(input == "A")  {     cout << "Congratulations, you followed the instructions!" << endl;     cin.get();  }  else  {     cout << "Incorrect input!" << endl;     cin.get();  }  if(input == "a")  {    cout << "You did it correctly, but you didn't capitilize!" << endl;    cin.get();  }  else  {     cout << "Incorrect input!" << endl;     cin.get();  }  cin.get();    return 0;}

The using namespace std; just includes our commands... such as cout << to output text to the DOS screen... well if you didnt use that, you'd have to do.. std::cout << .

##### Share on other sites
SpacedOut    138
shouldn't:

if(input == "A")

be:

if(input == 'A')

It's single quotes for characters, double quotes for strings.

##### Share on other sites
Zaris    183
What type of material are you using for learning C++?

##### Share on other sites
Ezbez    1164
Quote:
 Original post by NIABwell, except what the namespace std is,and the iostream thing.

"using namespace blarg;" means that everything inside of the namespace titled "blarg" will be taken out of that namespace and put into your regular 'global' namespace. A namespace is a group of code (functions, classes, anything!) that are put into a separate 'box' from the rest of the program. By writing 'using namespace whatever' it takes all the parts of the code out of the box and lets you use it without specifically specifying that it's in the box.

Bad analogy time! I've got a hammer, it is in a box, let's call it "box std". If I want my assistant to get a hammer, I have to tell him where the hammer is! So, I tell him to get me my hammer out of the box std. Now, if I was to tell him to first dump the contents of the box out, then I wouldn't have to tell him to look in the box to find the hammer since it'd already be out. That's what "using namespace std;" does; it dumps box std out onto the ground.

#include <iostream> is a bit simpler. It is telling the compiler to copy the contents of the file "iostream" into your code. iostream contains all the information that is required to use cout, cin and other things. Your compiler provides you with iostream, so you don't have to write that, but you do have to tell it to use the file, otherwise it won't know what cout and cin are.