Sign in to follow this  
NIAB

how to classify a variable?

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

char input; // define input
cout << "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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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;
cout<<"please type 'A' \n"<<input<<endl;
if (input== 'a')
{
cout<<"good job! you typed 'A' \n"<<endl;
}
else
{
cout<<"you failed typing 'A' \n"<<endl;
}
return 0;
}

Share this post


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

cout<<"Please type a: "<<endl;
cin>>input;

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by NIAB
well, 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this