C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 6th, 2006, 09:04 AM
1076 1076 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 2 1076 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 50 sec
Reputation Power: 0
Help with SIMPLE programming

I'm a very new programmer with C++, and I wrote a very simple else-if code based on the "guess the number" program, this morning. I have the user guess a birthday, but at the moment, the program crashes when a user enters anything other than an integer. I realize that this is because I have the input identified as an integer, but I want to be able to have the program recognize both integers and alphabetical text, so that I can program responses to either. At the moment, if any alphabetical text is put in, the program crashes.
Any help on this matter would be appreciated. But please remember that I just started at the beginning of this week, so you man lose me on some terms and things. Feel free to talk down to me.

My program:
Code:
#<iostream>
using namespace std;

int main ();

{
	constint MONTH=6;
	constint DAY=23;
	int guessMonth;
	int guessDay;
	int something; 

	cout << "Can you guess my birthday? /n";
	cout << "Month";
	cin >> guessMonth;
	cout << "Day";
	cin >> guessDay;
	if (guessDay<=0)
	{
		cout <<"There aren't any negative days on my calender..."<< endl;
	}
	else if (guessDay>31)
	{
		cout <<"Most months don't have more than 31 days, I'm afraid"<< endl;
	}
	else if (guessMonth<=0)
	{
		cout << "The year starts with month ONE."<< endl;
	}
	else if (guessMonth>12)
	{
		cout << "So exactly what is the name of that month?" << endl;
	}
	else if (guessMonth==MONTH && guessDay==DAY) 
	{
		cout << "ALRIGHT! You did it!" << endl;
	}
	
	else if (guessMonth>MONTH && guessDay>DAY)
	{ 
		cout << "Woah! Both of those are too high!" << endl;
	}

	else if (guessMonth>MONTH && guessDay<DAY)
	{
		cout << "Tsk. Month's too high, and the day's too low!" << endl;
	}

	else if (guessMonth<MONTH && guessDay>DAY)
	{
		cout << "Aww, the month's too low, and the day's too high!" << endl;
	}

	else if (guessMonth>MONTH && guessDay>DAY)
	{
		cout << "Man, both those are just too high." << endl;
	}

	else if (guessMonth==MONTH && guessDay>DAY)
	{
		cout << "Well, you've got the month, but try a lower day." << endl;
	}

	else if (guessMonth==MONTH && guessDay<DAY)
	{
		cout << "Shoot! You got the month, but try a higher day." << endl; 
	}

	else if (guessMonth<MONTH && guessDay==DAY)
	{
		cout << "Well, you've got the day, now go for a higher month." << endl;
	}

	else //
	{
		cout << "Almost! You've got the day, but try a lower month." << endl;
	}
	cin >> something;
	return 0;
}


Thanks in advance!

1076

Reply With Quote
  #2  
Old April 6th, 2006, 04:02 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 14
Send a message via AIM to ubergeek
if you want to do that, you'll have to take input as a string and parse it yourself.
the isdigit() and isalpha() functions in <cctype> and the atoi() function from <cstdlib> (or stringstreams if you want to delve into that)
will be useful for testing string content and converting from string to number. Also, the strtol function converts a string to an integer and also gives you a pointer to where in the string the numeric characters ended.

Reply With Quote
  #3  
Old April 7th, 2006, 01:17 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
well first off if you want to make a constant variable you have to declare it as a const <- not a constint. You could have just typed it wrong in the post but if you copy and pasted then there is one problem. I adjusted some of the code and take a look at it and see if it makes sense I put a goto statement in there so you can play until you guess it right if your that patient.
RED is what I changed.
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
	const int MONTH=6;
	const int DAY=23;
	int guessMonth=0;
	int guessDay=0;
	int something=0; 

	cout << "Can you guess my birthday? \n";
	top://returns here if something == 1
	cout << "Month :";
	cin >> guessMonth;
	cout << "Day :";
	cin >> guessDay;
	if (guessDay<=0)
	{
		cout <<"There aren't any negative days on my calender..."<< endl;
	}
	else if (guessDay>31)
	{
		cout <<"Most months don't have more than 31 days, I'm afraid"<< endl;
	}
	else if (guessMonth<=0)
	{
		cout << "The year starts with month ONE."<< endl;
	}
	else if (guessMonth>12)
	{
		cout << "So exactly what is the name of that month?" << endl;
	}

	if  (guessMonth==MONTH && guessDay==DAY) 
	{
		cout << "ALRIGHT! You did it!" << endl;
	}
	
	else if (guessMonth>MONTH && guessDay>DAY)
	{ 
		cout << "Woah! Both of those are too high!" << endl;
	}

	else if (guessMonth>MONTH && guessDay<DAY)
	{
		cout << "Tsk. Month's too high, and the day's too low!" << endl;
	}

	else if (guessMonth<MONTH && guessDay>DAY)
	{
		cout << "Aww, the month's too low, and the day's too high!" << endl;
	}

	else if (guessMonth>MONTH && guessDay>DAY)
	{
		cout << "Man, both those are just too high." << endl;
	}

	else if (guessMonth==MONTH && guessDay>DAY)
	{
		cout << "Well, you've got the month, but try a lower day." << endl;
	}

	else if (guessMonth==MONTH && guessDay<DAY)
	{
		cout << "Shoot! You got the month, but try a higher day." << endl; 
	}

	else if (guessMonth<MONTH && guessDay==DAY)
	{
		cout << "Well, you've got the day, now go for a higher month." << endl;
	}

	else //
	{
		cout << "Almost! You've got the day, but try a lower month." << endl;
	}
	cout<<"Would you like to try again\n"
	    <<"1] YES\n"
	    <<"2] NO\n";
    cin>>something;
    if(something==1);
    goto top;//returns user to beginning
	system("PAUSE");
	return 0;
}

You could also use a while loop as well but I didn't want to push it, dont want you to not jump to far ahead of what your reading or studying. =)
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Last edited by Geo.Garnett : April 7th, 2006 at 01:20 AM.

Reply With Quote
  #4  
Old April 7th, 2006, 09:11 AM
1076 1076 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 2 1076 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 50 sec
Reputation Power: 0
ubergeek:

Man, I really wish I knew what you were talking about. Sounds like damn good advice. I'll try to look up some of those terms, but it's pretty difficult when defining a piece of syntax uses six other words/phrases that I've never heard of. @.@
Get's pretty crazy, pretty quick.

Geo.Garnett:

Hey, thanks a lot! Actually, my compiler is Dev C++, and it allows 'constint', as I did copy and paste, straight from the compiler screen. But, thanks for the heads up. And, speaking of "While" statements, that's what I went into, so I just set up a while(true) infinite loop. Although, I like your way much better, as it gives a Yes/No option. Thanks much, man. But, do you think you could explain how to allow a letter or a number variable? Like I said, my game crashes when an answer like "no" appears. Of course, I understand WHY it's doing that, because it's only programmed to deal with integers, but I can't figure out how to make it want to deal with either letters or numbers.
Could I ask you to post a simple code that would allow that, and then decipher why each part of the code allows it? Kinda like how you did with the goto 'top' statements? Not neccesarily as an edit to my code, but just a standalone peice would be fine. I mean, before that I had no idea what a goto statement was (my teacher and book say that they are precursory to spaghetti code :<), but just by you using the code dynamically, and explaining it quickly, like that, I'm pretty confident I've grasped the basics of a goto statement, and how to use it. If you could, that would be a GREAT help to me.

But, if not, I appreciate your help, thus far! Thanks alot!

1076

Reply With Quote
  #5  
Old April 7th, 2006, 11:34 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
Ok about the goto use first. The goto is usually frowned on depending on the usage. Reason being is because it can become confusing if you use it several times throughout a long program. Just imagine if you had a program that had hundreds of lines of code and you used four or five goto statements through out the program it would become hard to follow, so that's why they usually tend to find other work around.

Now as far as the other question, I'm not sure if your asking to post code that pertains to asking for text input such as a string or a char[] and comparing it or asking if you could set up a variable to use for either.

If you are just talking about having the user input a word to choose a selection such as a yes or no question then well that's fairly simple. Look at the code below.
NOTE = It seems as if you are already further along then this in your books so this might just be a refresher if this is actually what you were asking
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
	char choose[2];
		while(!stricmp(choose,"yes") || stricmp(choose,"no"))
		//Keeps you in the loop until yes or no is input
	{
	cout<<"Would you like to quit or proceed? :";
	cin>>choose;
	if(!stricmp(choose,"yes"))//compares input to see if yes was typed
	//stricmp function works with char[] arrays only
	{
       cout<<"Congratulations you chose "<<choose<<endl;
    }
       if(!stricmp(choose,"no"))
       //stricmp function determines no, no matter the case.
       //strcmp is case sensitive
       {
           cout<<"Congratulations you chose "<<choose<<endl;
       }
     }
	system("PAUSE");
	return 0;
}

Now if you were wondering if you could make a question that determines if its an integer or a number well there's a few ways to do this, you can just compare the strings as I did above or you can determine the ASCII code for the numbered characters which unless you know them or have a chart of them its not always the easiest way to do it. I prefer my way because I'm lazy and not very smart and work with what little brain God has provided me. lol .So I hope I answered your questions and if not just clarify it a little cause like I just said I'm a little slow

As far as your compiler letting you declare the const int a constint that cool, I wasn't aware of that, I use Dev C++ mainly but I also have MS Visual Studio (Which kinda sucks I might add) and it errors If I try to do that unless I #define it that way =).

As if this post isn't long enough, you can also compare strings in C++ by using the == signs =) but doesn't really give you the functionality of the stricmp.
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
    string choose;
    cout<<"Choose yes or no, and yes this is case sensitive : ";
    cin>>choose;
    if(choose == "yes")
    {
              cout<<"congratulations you chose "<<choose<<endl;
              }
    if(choose == "no")
    {
              cout<<"congratulations you chose "<<choose<<endl;
              }
	system("PAUSE");
	return 0;
}

Use what you wish thats the beauty of programming there so many possibilities.

Last edited by Geo.Garnett : April 7th, 2006 at 11:39 PM.

Reply With Quote
  #6  
Old April 8th, 2006, 11:59 AM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 14
Send a message via AIM to ubergeek
I'll try to make a simple example of what I meant...Sorry for my bad explanation:
Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string input = "";
	cout << "Please enter a number: ";
	getline(cin, input); //get user input to string input, including any spaces that the user typed (simply cin>> would stop at a space; getline stops at a line break)
	char *end_of_str = NULL; //strtol tells us where in the string the number ended
	long number = strtol(input.c_str(), &end_of_str, 0); //the 0 tells strtol to use the first character of the string to determine the number's base (if it starts with 0, the number is assumed to be in octal. if the input starts with 0x, it is hexadecimal. otherwise, it is decimal)
	if (end_of_str - input.c_str() == 0) //if the number "ends" at the beginning of the string -- in other words, if there was no number
	{
		cout << "Dammit! I told you to enter a number!" << endl;
		cin.get();
		return 1;
	}
	cout << "You entered " << number << endl;
	cin.get();
	return 0;
}

//OUTPUT (user-typed text in italics):
C:\>test_strtol
Please enter a number: no
Dammit! I told you to enter a number!
[return]

C:\>test_strtol
Please enter a number: 34
You entered 34
[return]

C:\>test_strtol
Please enter a number: 2 and a half
You entered 2

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with SIMPLE programming


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap