Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strings coparison

Status
Not open for further replies.

ah01

Programmer
Jan 29, 2005
4
0
0
US
#include <iostream.h>
#include <string.h>

int main()
{
char name1[15];
char name2[15];

bool game = false;
int answer = 1;

while (!game)
{

cout << "enter name1 : " ;
cin.getline(name1 , 5);
cin.ignore(80,'\n');

cout << "enter name2 : " ;
cin.getline(name2 , 5);
cin.ignore(80,'\n');

cout << "name1 = " << name1 << endl;
cout << "name2 = " << name2 << endl;

int y = strcmpi(name1 , name2);

if (!y)
{
cout <<"names are identical" << endl;

}
else
{
cout << "names are different" << endl;

}

cout << "do you want to continue? (1=yes ,2=no ):";
cin >> answer;
if (answer == 2)

game = true;

}// end of while

return 0;
}// end of main
this sample code I wrote last year, but i can't figure out how to make it work the way I want.
when I run the above code under vc++ 6.0, the first time it compares the two strings and prints to the screen "identical or "different" message. The second time it does not allow me to put a string in name1?. This code suppose to compare two names inputed via the keyboard and asks if the user want to try again. It could have to do with memory buffer area?any help is appreciated.

 
> cin >> answer;
Follow this with
Code:
cin.ignore(80,'\n');

Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Minor comment to your program: use <iostream> instead of <iostream.h>. <iostream.h> is just for backward compatibility with very old (C++ V2) programs.
 
thaks, that fixed the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top