Jump to content
  • Advertisement
Sign in to follow this  
NIAB

how to classify a variable?

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!