GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Feb-2008, 04:55
utdemir utdemir is offline
Awaiting Email Confirmation
 
Join Date: Feb 2008
Posts: 15
utdemir is on a distinguished road

If "x" isn't a number...?


How can the program understand the variable is a number or a char?
  #2  
Old 14-Feb-2008, 05:34
davis
 
Posts: n/a

Re: If "x" isn't a number...?


Quote:
Originally Posted by utdemir
How can the program understand the variable is a number or a char?

What is the difference between a number and a char in C/C++?


:davis:
  #3  
Old 14-Feb-2008, 05:57
utdemir utdemir is offline
Awaiting Email Confirmation
 
Join Date: Feb 2008
Posts: 15
utdemir is on a distinguished road

Re: If "x" isn't a number...?


Quote:
Originally Posted by davis
What is the difference between a number and a char in C/C++?

I didn't undertand it. My problem is I want user to input a number, but if user input a char, my program is fail. I want program understood x is a char and warn user.
  #4  
Old 14-Feb-2008, 06:25
davis
 
Posts: n/a

Re: If "x" isn't a number...?


Quote:
Originally Posted by utdemir
I didn't undertand it. My problem is I want user to input a number, but if user input a char, my program is fail. I want program understood x is a char and warn user.

What are the possible integral values for a signed char? -128 to 127? Aren't those "numbers?" Do you have something against those particular numbers? Was there a specific number that you were looking for that can not be represented by -128 to 127?

Here's a good plan...go read the www.gidforums.com and then come back, post some of your "program" code so that we can figure out what the problem is without all of the ambiguous questions between here and there.


:davis:
  #5  
Old 14-Feb-2008, 06:52
utdemir utdemir is offline
Awaiting Email Confirmation
 
Join Date: Feb 2008
Posts: 15
utdemir is on a distinguished road

...


Thanks davis, i put here my code. Sorry, i am new on forum.

Here is my code:

CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{
    system("color 7c");
    
	char isim[20];
	int yas;
	int tahmin;
	int tahminsayisi;
	char istek;
	
	srand(time(0));

    int random = rand()%100;
    int a = rand()%3;
	int b = rand()%2;

	
	cout<<"************************************************"<<endl;
	cout<<"**                                            **"<<endl;
	cout<<"**               Chat Botu v1.0               **"<<endl;
	cout<<"**                                            **"<<endl;
	cout<<"**                  by Cleon                  **"<<endl;
	cout<<"**                                            **"<<endl;
	cout<<"************************************************"<<endl;
    cout<<"Not:"<<endl<<"Programi kullanirken Turkce karakter kullanmayin..."<<endl<<endl<<endl; //not to use turkish character
	
	
cout<<"Merhaba. Isim?"<<endl; // hi.. name?
cin>>isim;
	
cout<<isim<<", guzel..."; /good
cout<<"Yas kac "<<isim<<"?"<<endl; // how old are you?


	cin>>yas;

cout<<endl;

if(yas<0)
cout<<"Daha gozlerini acmana bile "<<yas*(-1)<<" sene var..."<<endl<<"Simdiden bilgisayara merak salip bozma o gozlerini bence..."<<endl<<endl;
else if(yas < 10) 
cout<<"Vay be... Bu yasta bu zeka... Helal..."<<endl<<endl;
else if(yas > 80)
cout<<"Sen "<<yas<<" yasindaysan ben de abakusum."<<endl<<"Biraz  mantikli sallasaydin bari..."<<endl<<endl;
else if(yas > 50)
cout<<"Size dede diyebilir miyim? Takdir ettim bu yasta yeni seylere kayitsiz kalmamanizi..."<<endl<<"Neyse bosverin.."<<endl<<endl;

else
{   b = rand()%2;
			
			if(b==1) 
			cout<<"Tesadufe bak..."<<endl<<"Benim bir arkadasim var, o da "<<yas<<" yasinda... :-)"<<endl<<endl;	
			
			if(b==0)
			cout<<"Ne hos... Ogrenci misin bari "<<isim<<"?"<<endl<<"Neyse bosver.. Ne fark edecek ki..."<<endl<<"Sormadim farzet..."<<endl<<endl;
}			
    
//sayi tahmin oyunu başlangıç //game start
	
	cout<<"Hadi bir oyun oynayalim en iyisi biz..."<<endl; //lets play a game
oyunbasi:
	cout<<"Bir sayi tuttum ben 1-100 arasinda, tahmin et bakalim..."<<endl<<endl;//i keep a nuber in my mind, what is this?
tahminsayisi=0;
	{
adim:
	cout<<"==> ";
	cin>>tahmin; //here, if user input a char, program will fail...
	
                      
	
		if( (tahmin > 100) || (tahmin<0) )
		
			{
			a = rand()%3;
			
			if(a==1) 
			cout<<"Kendine gel... 1-100 arasi dedik..."<<endl<<endl;	
			
			if(a==2)
			cout<<"Hoopp. Bu ne simdi?"<<endl<<endl;
			
			if(a==0)
			cout<<"Alay mi ediyorsun sen benimle?"<<endl<<endl;
			
			tahminsayisi=tahminsayisi+1;
			
			goto adim;
			}
	
	
		else if(tahmin < random)
			{
			a = rand()%3;
			
			if(a==1) 
			cout<<"Daha yukari cikman lazim..."<<endl<<endl;
			
			if(a==2)
			cout<<"Yukari yukari..."<<endl<<endl;
			
			if(a==0)
			cout<<"Cik..."<<endl<<endl;
			
			tahminsayisi=tahminsayisi+1;
			
			goto adim;
			}
		else if(tahmin > random)
			{
			a = rand()%3;
			
			if(a==0)
			cout<<"In.."<<endl<<endl;
			
			if(a==1)
			cout<<"Asagi.."<<endl<<endl;
	
			if(a==2)
			cout<<"Yavas...  Daha dusuk..."<<endl<<endl;
			
			tahminsayisi=tahminsayisi+1;
			
			goto adim;
			}
		else if(tahmin == random)
			{
			a = rand()%3;
		
			if(a==1)
			cout<<"Afferim..."<<endl<<endl;
		
			if(a==2)
			cout<<"Iste bu kadar!"<<endl<<endl;
		
			if(a==0)
			cout<<"Budur..."<<endl<<endl;
			}		
    }

cout<<(tahminsayisi+1)<<" tahminde buldun..."<<endl<<endl;

cout<<"Tekrar oynamak ister misin "<<isim<<"? (e / h)"<<endl; //are you want to play again (y-n)
cin>>istek;

istek:

switch(istek)
{
case 'e':
     {
	cout<<"Tamam "<<isim<<", sen nasil istersen, basliyoruz.."<<endl<<endl;
	random = rand()%100;
    goto oyunbasi;
    break;
    }             
case 'h':
   {
   cout<<"Keyfin bilir "<<isim<<".. Oynamak istersen ben buradayim..."<<endl<<endl;
   goto oyunsonu;
   break;
   }                   
}

{
	cout<<"Tekrar oynamak istiyorsan e , istemiyorsan h gir, ugrastirma simdi beni..."<<endl;
	cout<<"e / h --> ";
	cin>>istek;
	goto istek;
}


//sayi tahmin oyunu son
oyunsonu:


	system("PAUSE");
    return EXIT_SUCCESS;
}
  #6  
Old 14-Feb-2008, 07:03
utdemir utdemir is offline
Awaiting Email Confirmation
 
Join Date: Feb 2008
Posts: 15
utdemir is on a distinguished road

Re: If "x" isn't a number...?


Program failing in line 72. I wrote the means... If user wrote a char in question at line 72...
  #7  
Old 14-Feb-2008, 07:04
davis
 
Posts: n/a

Re: If "x" isn't a number...?


CPP / C++ / C Code:
cin>>tahmin; //here, if user input a char, program will fail...

So, what is the value of tahmin after the user inputs a char?

...once you know what happens as a result of cin trying to convert the user input to an int, you will better understand how to deal with the problem.


:davis:
  #8  
Old 15-Feb-2008, 07:49
utdemir utdemir is offline
Awaiting Email Confirmation
 
Join Date: Feb 2008
Posts: 15
utdemir is on a distinguished road

Re: If "x" isn't a number...?


I tried, cin converts char to "2"... I can solve the problem with
CPP / C++ / C Code:
if(tahmin==2)
goto oyunbasi;
But if user input 2, what happens? User can't input "2"..

Maybe, I appear stupid, but i can't solve it...
Thanks Davis. for your answers..

edit: Sorry, this couldn't solve it, too...
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Number Generator Help jj9187 C Programming Language 2 12-Feb-2007 23:25
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 16:48
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 12:15
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 19:44
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:11.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.