9 Replies - 2017 Views - Last Post: 18 March 2010 - 06:03 AM Rate Topic: -----

#1 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Compare contents of array with input

Posted 16 March 2010 - 08:21 PM

I am asked to make a program in Turbo C++ which receives the answer inputted and tells if it is correct or not. But the problem is that it always show incorrect even if the input is correct.

Here is my code:

#include<iostream.h>
#include<conio.h>

main()
{
clrscr();
char answer[10];
cout<<"What is the capital city of Finland\n";
cout<<"Answer: ";
cin>>answer;
if(answer=="Helsinki")
	{
	cout<<"You are right!\n";
	cout<<"Your score:\n";
	cout<<"Correct: 1\n";
	cout<<"Incorrect: 0\n\n";
	cout<<"Press any key to continue...";
	}else
	{
	cout<<"You are wrong!\n";
	cout<<"Your score:\n";
	cout<<"Correct: 0\n";
	cout<<"Incorrect: 1\n\n";
	cout<<"Press any key to continue...";
	}
getch();
return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Compare contents of array with input

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Compare contents of array with input

Posted 16 March 2010 - 08:47 PM

Tell your teacher to get a better compiler.

Anyway, you can't compare strings like that.
Was This Post Helpful? 1
  • +
  • -

#3 wheelieInCali  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 16-March 10

Re: Compare contents of array with input

Posted 16 March 2010 - 08:47 PM

I don't know what C++ Turbo is but try including the cstring library and using the functions there.

This post has been edited by wheelieInCali: 16 March 2010 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Compare contents of array with input

Posted 16 March 2010 - 08:54 PM

If it were a string like in java it is much easy to compare. But Turbo C++ won't recognize String values, so I have to use char array. Then that is the hard part because I won't match with the input.
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Compare contents of array with input

Posted 16 March 2010 - 10:09 PM

Use strcmp():
if (strcmp(answer, "Helsinki") == 0)
{
  // do something  here
}


http://www.cplusplus...cstring/strcmp/
Was This Post Helpful? 2
  • +
  • -

#6 sweet.k.angel  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 33
  • Joined: 18-January 10

Re: Compare contents of array with input

Posted 17 March 2010 - 01:59 AM

use strcmp(srt1, str2)
use header file -- string.h

this function compares str1 with str2 , and returns 0 if they are same.
Was This Post Helpful? 0
  • +
  • -

#7 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Compare contents of array with input

Posted 17 March 2010 - 09:16 AM

Guys my group and I revised the structure of our program into this code:

#include<String.h>
#include<Stdlib.h>
#include<Stdio.h>
#include<Time.h>
#include<iostream.h>
#include<conio.h>

char State[114][255] = {
								"Algeria",
								"Angola",
								"Burkina Faso",
								"Cameroon",
								"Canary Islands",
								"Central Africa",
								"Chad",
								"Congo",
								"Egypt",
								"Ethiopia",
								"Gabon",
								"Ghana",
								"Guinea",
								"Ivory Coast",
								"Kenya",
								"Liberia",
								"Libya"
								"Madagascar",
								"Malawi",
								"Mali",
								"Morocco",
								"Mozambique",
								"Niger",
								"Nigeria",
								"Senegal",
								"Sierra Leone",
								"Somalia",
								"South Africa",
								"Sudan",
								"Tanzania",
								"Togo",
								"Tunisia",
								"Uganda",
								"Zaire",
								"Zambia",
								"Zimbabwe",
								"Afganistan",
								"Bangladesh",
								"Burma",
								"Cambodia",
								"China",
								"Iran",
								"Iraq",
								"Israel",
								"Japan",
								"Jordan",
								"North Korea",
								"South Korea",
								"Kuwait",
								"Lebanon",
								"Malaysia",
								"Mongolia",
								"Nepal",
								"Pakistan",
								"Philippines",
								"Saudi Arabia",
								"Singapore",
								"Sri Lanka",
								"Syria",
								"Taiwan",
								"Thailand",
								"Turkey",
								"United Arab Emirates",
								"Vietnam",
								"North Yemen",
								"South Yemen",
								"Australia",
								"New Zealand",
								"Albania",
								"Austria",
								"Belgium",
								"Bulgaia"
								"Czechoslovakia",
								"Denmark",
								"Finland",
								"France",
								"Germany",
								"Greece",
								"Hungary",
								"Italy",
								"Nehterlands",
								"Norway",
								"Poland",
								"Portugal",
								"Romania",
								"Spain",
								"Sweden",
								"Switzerland",
								"Russia",
								"United Kingdom",
								"Yugoslavia",
								"Canada",
								"Costa Rica",
								"Cuba",
								"Dominican Republic",
								"El Salvador",
								"Guatemala",
								"Haiti",
								"Honduras",
								"Jamaica",
								"Mexico",
								"Nicaragua",
								"Panama",
								"United States of America",
								"Argentina",
								"Bolivia",
								"Brazil",
								"Chile",
								"Colombia",
								"Ecuador",
								"Paraguay",
								"Peru",
								"Uruguay",
								"Venezuela"};

char Capital[114][255] = {
								"Algiers",
								"Luanda",
								"Ouagadougou",
								"Douala",
								"Las Palmas",
								"Bagui",
								"Ndjamena",
								"Brazzaville",
								"Cairo",
								"Addis Ababa",
								"Libreville",
								"Accra",
								"Conakry",
								"Abidjan",
								"Nairobi",
								"Monrovia",
								"Tripoli",
								"Antanananivo",
								"Blantyre",
								"Bamako",
								"Casablanca",
								"Maputo",
								"Niamey",
								"Lagos",
								"Dakar",
								"Freetown",
								"Mogadishu",
								"Johannesburg",
								"Omdurman",
								"Dodoma",
								"Lome",
								"Tunis",
								"Kampala",
								"Kinshasa",
								"Lusaka",
								"Harare",
								"Kabul",
								"Dhaka",
								"Rangoon",
								"Phnom Penh",
								"Beijing",
								"Tehran",
								"Baghdad",
								"Jerusalem",
								"Tokyo",
								"Amman",
								"Pyongyang",
								"Seoul",
								"Kuwait",
								"Beirut",
								"Kuala Lumpur",
								"Ulan Bator",
								"Katmandu",
								"Karachi",
								"Manila",
								"Riyadh",
								"Singapore",
								"Colombo",
								"Damascus",
								"Taipei",
								"Bangkok",
								"Istanbul",
								"Dubai",
								"Ho Chi Minh",
								"Sana",
								"Aden",
								"Sydney",
								"Auckland",
								"Tirane",
								"Brussels",
								"Sofia",
								"Prague",
								"Copenhagen",
								"Helsinki",
								"Paris",
								"Berlin",
								"Athens",
								"Budapest",
								"Rome",
								"Rotterdam",
								"Oslo",
								"Warsaw",
								"Lisbon",
								"Bucharest",
								"Madrid",
								"Stockholm",
								"Zurich",
								"Moscow",
								"Lonodn",
								"Belgrade",
								"Ottawa",
								"San Jose",
								"Havana",
								"Santo Domingo",
								"San Salvador",
								"Guatemala",
								"Port-au-Prince",
								"Tegucigalpa",
								"Kingston",
								"Mexico",
								"Managua",
								"Panama",
								"Washington",
								"Buenos Aires",
								"La Paz",
								"Sao Paulo",
								"Santiago",
								"Bogota",
								"Quito",
								"Asuncion",
								"Lima",
								"Montevideo",
								"Caracas"};

int main()
{
clrscr();

	srand(time(0));

	int NumCorrect   = 0;
	int NumIncorrect = 0;
	int true;
	while(true)
	{
		int RandNum = rand() % 49;

		if(!strlen(State[RandNum]))
		{
			while(!strlen(State[RandNum]))
			{
				RandNum = rand() % 49;
			}
		}
		char Answer[255];
		char Buffer[255];

		sprintf(Buffer, "What is the capital of %s?", State[RandNum]);
		puts(Buffer);
		gets(Answer);

		if(!strcmp(Answer, Capital[RandNum]))
		{
			NumCorrect ++;

			if(NumCorrect == 50)
			{
				puts("You guessed all 50 state capitals!\n"
					 "You win!!! (-:");

				system("pause");

				exit(EXIT_SUCCESS);
			}

			strcpy(State[RandNum], "");
			strcpy(Capital[RandNum], "");
			printf("%s, was correct! Excellent Job!\n", Answer);
		}
		else
		{
			NumIncorrect ++;

				if(NumIncorrect == 3)
			{
				puts("You got three (3) answers incorrect.\n"
					 "You lose!!! )-:");

				system("pause");

				exit(EXIT_SUCCESS);

			}

			printf("%s, was incorrect. The correct answer was %s.\n", Answer, Capital[RandNum]);
		}

		printf("Your score is \n"
				"Correct      : %i\n"
				"Incorrect    : %i\n\n", NumCorrect, NumIncorrect);

		system("pause");
		system("cls");
	}

	system("pause");

	return(0);




Unfortunately there are errors on this part:

	srand(time(0));


Error Statement: Conversion may lose significant digits in function main()

And the second error is here:

	while(true)


Error Statement: Possible use of 'true' before definition in function main()

What is your suggestion/comment on this? Please help.
Was This Post Helpful? 0
  • +
  • -

#8 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Compare contents of array with input

Posted 17 March 2010 - 11:57 AM

For srand, look at [ This Page ]

And for the while loop, try: while(1). Technically you can use any number other than 0, so it represents a true condition.

Also, your headers starting with capital are wrong, they must be all non-capital.

This post has been edited by Anarion: 17 March 2010 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Compare contents of array with input

Posted 17 March 2010 - 10:15 PM

Why are there so many new lines?
Was This Post Helpful? 0
  • +
  • -

#10 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Compare contents of array with input

Posted 18 March 2010 - 06:03 AM

Thanks guys for the comments. I figured out the problem. The codes are good, it was the compiler that is not compatible with the code. I tried it on different compiler and it compiled and run. Thanks for the help anyways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1