|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
im attempting to teach myself c++ due to the fact that the current highschool i attend has dropped any sort of computer teaching classes due to the NYC cutbacks
so, you guys (and gals ) will hear alot more from me. anyways, heres my first question!i want this program to behave almost as a password system. if the proper name (patrick) isnt entered, then it tells you that you are not a legal user and shuts down. i've reached my first hurdle, i cant make the program say anything else except what is supposed to be said when the correct name is entered, even when the name is incorrect! what am i doing wrong? #include <iostream> int main() { int patrick,a,c; a=1,patrick=1,c=2; a=patrick; std::cout<<"Enter your name\n"; std::cin>>a; if (a==patrick) { std::cout<<"This is your name\n"; } else { std::cout<<"you are not patrick! > \n";} std::cout<<"Goodbye \n";int close; std::cout<<"please close the program.\n"; std::cin>> close; return 0; } also, i want the program to prompt you to close the program by typing "close" but it closes on its own! all help will be apreciated! ![]() |
|
#2
|
||||
|
||||
|
I'm confused. You say you want the user to enter his name, yet the variables are integers! Are you entering numbers or names?
Running it again as it is, try entering numbers and see what it does. Now, I'm not sure what cin does when it's told to expect a number and gets letters instead. The reason why it seems to blow through that cin<<close; could be that it thinks there are still characters in the input buffer. Also, you shouldn't need to preface every cout and cin with std:: . Namespaces came out after my time, but I think there's a using statement you can put at the start of the program. |
|
#3
|
|||
|
|||
|
i thought that i had to make the variables integers
im not really sure what im doing, also im not sure what the buffer thing is (didnt get up to that chapter yet ) but, what am i supposed to do if the variables are all words (names) how do i define them? i didnt know how to do that so i defined them with numbers. im not sure about alot of things. how can i fix this so that i use only words, not numbers and that it will recognize the correct name? ![]() |
|
#4
|
||||
|
||||
|
Something like this perhaps.
Code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string name;
cout<<"Enter your name\n";
getline(cin, name);
if (name=="patrick")
{
cout<<"This is your name\n";
}
else
{
cout<<"you are not patrick! >:( \n";
}
cout<<"Goodbye :)\n";
return 0;
}
The using namespace std; statement saves you the trouble of typing std:: in front of cin, cout etc., because it qualifies the namespace for you. If you don't understand the rest, keep reading the book and it'll all make sense sooner or later. Hope this helps! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > help with simple if statements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|