I'm having problem w/ this codes in bright red, when i'm trying to enter a name the first two letters are always not shown or when inside the inner looping, it don't show what the inner looping is containing.

#include <iostream>
#include <iomanip>
#include <cstdio> // used for reading arrays w/ white spaces or "space'. using gets()using namespace std;
int main()
{
	char name[80];// declares an array to hold 50 character strings 
	int y,x,a, b;

x=3;
while(x=3)

	{
		cout<<"\t main menu\n";
		cout<<"[1] Play game\n";
        cout<<"[2] Program Objectives\n";
		cout<<"[3] Top score\n";
		cout<<"[4] Quit\n";
		cout<< "enter the # of choice";
		cin>>y;
		cin.ignore();
		cin.ignore();
		system("cls");

		switch(y)
		{
		case 1:
			y=1;
			x=3;
			break;
		case 2:
			y=2;
			x=3;
			break;
		case 3:
			y=3;
			x=3;
			break;
		case 4:
			y=4;
			x=4;
			break;
		}
	if(y==1)
	{
		cout<<"\t main menu\n";
		cout<<"[1] Play game\n";
        cout<<"[2] back\n";
	
		cout<< "enter the # of choice";
		cin>>a;
		cout<<"input your name: ";
		gets(name);// here is the gets()
		cin.ignore();
		cin.ignore();
		system("cls");
		switch(a)
		{
		case 1:
			a=1;
			y=1;
			break;
		case 2:
			a=2;
			x=3;
			break;
	
		}
	if(a==1)
	{
		cout<<"\t main menu\n";
		cout<<"[1] 1-10\n";
        cout<<"[2]1-15\n";
		cout<<"[3]1-20n";
		cout<< "enter the # of choice";
		cin>>b;
		switch(b)
		{
		case 1:
			b=1;
			y=1;
			break;
		case 2:
			b=2;
			y=1;
			break;
		case 3:
			b=3;
			y=1;
			break;
		
		}
	if(b==1)
	{
		cout<<"1-10\n";
		cout<<"you choose this";
		cin.ignore();
		cin.ignore();
		system ("cls");
	y=1;
	
	}
	if(b==2)
	{
    cout<<"1-15";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	y=1;
	}
	if(b==3)
	{
	cout<<"1-20";
	cin.ignore();
	cin.ignore();
	system("cls");
		y=1;
		
	}
	



}



		
	
	}
	if(a==2)
	{
    cout<<"back to main menu:";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	x=3;
	}
	}

	
	if(y==2)
	{
    cout<<"\t\t\t ---------------------\n";
    cout<<"\t\t\t Program Objective\n";  
	cout<<"\t\t\t ---------------------\n\n";
    cout<<"   The objective of this project is for the proper application of\n"; 
	cout<<"our learned knowledge on C++. In this project we will try to put in all\n";
	cout<<"the things taught to us by our professor in lab and lecture, and also \n";
	cout<<"for the user to have fun.";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	x=3;
	}
	if(y==3)
	{
	cout<<"topscore";
	cin.ignore();
	cin.ignore();
	system("cls");
		x=3;
		
	}
	if(y==4)
	{
		cout<<"goodbye";
		cin.ignore();
		cin.ignore();
		x=4;
	}





return 0;
}

Recommended Answers

All 8 Replies

you don't need cstdio header file, and never ever use gets() even in c programs. Use getline() in c++ to get strings

cout<<"input your name: ";
cin.getline(name, sizeof(name));

You don't have to use cin.ignore() after getline(), only after entering integers

int x;
cin >> x;
cin.ignore();

read this thread for a more complete discussion.

you don't need cstdio header file, and never ever use gets() even in c programs. Use getline() in c++ to get strings

cout<<"input your name: ";
cin.getline(name, sizeof(name));

You don't have to use cin.ignore() after getline(), only after entering integers

int x;
cin >> x;
cin.ignore();

read this thread for a more complete discussion.

but if i don't use gets() the white space will not be read. The space for example

_________

enter name: joey marc
the name is joey

the marc won't be shown because of the "white space"

whats the difference w/ getline() and gets()?

>>but if i don't use gets() the white space will not be read. The space for example
WRONG. getline() reads everything including white space up to the '\n' character.

>>whats the difference w/ getline() and gets()?
getline() is c++ and works perfectly. gets() is old C and will trash your program if you are not very very careful when you type (don't type more characters than the input buffer can hold).

cout<<"input your name: ";
cin.getline(name, sizeof(name));

can you please explain further the red part. I can't really understand I'm only a newbie in c++ and self thought actually. that's why I don't how to use gets()

and the difference w/ gets() and cin.getline(name,size of (name))?
is there any header for getline?

getline() is declared in <iostream>

In the red part you posted, the first parameter to getline() is the name of the character array to receive the characters you type, similar to the first parameter to gets(). The second parameter is the maximum number of characters allowed in the input buffer. The sizeof is an operator that returns the size of the array. So if you defined char name[80]; then sizeof will return 80.

getline() is declared in <iostream>

In the red part you posted, the first parameter to getline() is the name of the character array to receive the characters you type, similar to the first parameter to gets(). The second parameter is the maximum number of characters allowed in the input buffer. The sizeof is an operator that returns the size of the array. So if you defined char name[80]; then sizeof will return 80.

so if cin.getline(name, size of(80)) the "size of(80)" here is the same as char name[80]? right?

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	
	int y,x,a,b;
    char name;
x=3;
while(x=3)

	{
		cout<<"\t main menu\n";
		cout<<"[1] Play game\n";
        cout<<"[2] Program Objectives\n";
		cout<<"[3] Top score\n";
		cout<<"[4] Quit\n";
		cout<< "enter the # of choice";
		cin>>y;
		cin.ignore();
		cin.ignore();
		system("cls");

		switch(y)
		{
		case 1:
			y=1;
			x=3;
			break;
		case 2:
			y=2;
			x=3;
			break;
		case 3:
			y=3;
			x=3;
			break;
		case 4:
			y=4;
			x=4;
			break;
		}
	if(y==1)
	{
		cout<<"\t main menu\n";
		cout<<"[1] Play game\n";
        cout<<"[2] back\n";
	
		cout<< "enter the # of choice";
		cin>>a;
		cout<<"input your name: ";
		cin.getline( name,sizeof(51));// here is the gets()

		
		switch(a)
		{
		case 1:
			a=1;
			y=1;
			break;
		case 2:
			a=2;
			x=3;
			break;
	
		}
	if(a==1)
	{
		cout<<"\t main menu\n";
		cout<<name;
		cout<<"[1] 1-10\n";
        cout<<"[2]1-15\n";
		cout<<"[3]1-20n";
		cout<< "enter the # of choice";
		cin>>b;
		switch(b)
		{
		case 1:
			b=1;
			y=1;
			break;
		case 2:
			b=2;
			y=1;
			break;
		case 3:
			b=3;
			y=1;
			break;
		
		}
	if(b==1)
	{
		cout<<"1-10\n";
		cout<<"you choose this";
		cin.ignore();
		cin.ignore();
		system ("cls");
	y=1;
	
	}
	if(b==2)
	{
    cout<<"1-15";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	y=1;
	}
	if(b==3)
	{
	cout<<"1-20";
	cin.ignore();
	cin.ignore();
	system("cls");
		y=1;
		
	}
	



}



		
	
	}
	if(a==2)
	{
    cout<<"back to main menu:";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	x=3;
	}
	}

	
	if(y==2)
	{
    cout<<"\t\t\t ---------------------\n";
    cout<<"\t\t\t Program Objective\n";  
	cout<<"\t\t\t ---------------------\n\n";
    cout<<"   The objective of this project is for the proper application of\n"; 
	cout<<"our learned knowledge on C++. In this project we will try to put in all\n";
	cout<<"the things taught to us by our professor in lab and lecture, and also \n";
	cout<<"for the user to have fun.";
	
	cin.ignore();
	cin.ignore();
	system("cls");
	x=3;
	}
	if(y==3)
	{
	cout<<"topscore";
	cin.ignore();
	cin.ignore();
	system("cls");
		x=3;
		
	}
	if(y==4)
	{
		cout<<"goodbye";
		cin.ignore();
		cin.ignore();
		x=4;
	}





return 0;
}

I tried the getline() here but an error was found.

error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(c
har *,int)' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

what should I do?

line 8: you changed it wrong. What you now have is just a single character. Look at your previous program and change it back to the way you had it before. char name[80]; >>so if cin.getline(name, size of(80)) right?
Wrong. its sizeof(name) ,

>>the "size of(80)" here is the same as char name[80]?
Nope.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.