how to classify a variable?

Started by
9 comments, last by Ezbez 17 years, 4 months ago
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?
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.
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";
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.
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;
}
you need to use cin for input. You can't combine it in the cout statement.

cout<<"Please type a: "<<endl;
cin>>input;
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.
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 << .
shouldn't:

if(input == "A")

be:

if(input == 'A')

It's single quotes for characters, double quotes for strings.
What type of material are you using for learning C++?

This topic is closed to new replies.

Advertisement