SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 26th, 2003, 07:55 PM
solidsnake solidsnake is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 2 solidsnake User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile help with simple if statements

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!

Reply With Quote
  #2  
Old April 26th, 2003, 08:40 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
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.

Reply With Quote
  #3  
Old April 26th, 2003, 09:11 PM
solidsnake solidsnake is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 2 solidsnake User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old April 27th, 2003, 12:31 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > help with simple if statements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway