What's the error?

Pages: 12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
	clrscr();
	char Country[20][20], Input[20], Temp[20];
	int n, count=0;
	do
	{
	 cout<<"Enter number of countries you want to enter: ";
	 cin>>n;
	}while(!(n>0)); 
	cout<<endl;
	cout<<"Enter the countries:"<<endl;
	for(int i=0; i<n; i++)
		gets(Country[i]);
	cout<<endl;
	cout<<"The countries you entered are:"<<endl;
	for(i=0; i<n; i++)
		cout<<Country[i]<<endl;
	cout<<endl;
	cout<<"The countries you entered in descending alphabetical order are:"<<endl;
	for(i=0; i<(n-1); i++)
	{
		for(int j=0; j<(n-i-1); j++)
		{
			if(strcmpi(Country[j],Country[j+1])<0)
			{
				strcpy(Temp,Country[j]);
				strcpy(Country[j],Country[j+1]);
				strcpy(Country[j+1],Temp);
			}
		}
	}
	for(i=0; i<n; i++)
		cout<<Country[i]<<endl;
	cout<<endl;
	cout<<"Enter the name of the country you want to check: ";
	gets(Input);
	cout<<endl;
    for(i=0; i<n; i++)
	{
		if(!(strcmp(Country[i],Input)))
			count++;
	}
   if(count==1)
	   cout<<"The country you entered is registered."<<endl;
   else if(count)
	   cout<<"The country you entered is not registered."<<endl;
   getch();
}


The compiler gives and error in:
line 9: saying "Size of Country is unknown or zero"
line 32, 33, 34: saying "Lvalue required"
But for the error in line 8, I am making the user enter the value through "cin>>n;" where n is an integer as defined in line 10. So why is the error coming?
For the rest (32, 33, 34) I don't know what to do! Need help urgently!
Last edited on
char Country[/*you have missed size here*/][20]Arrays in C++ should know their size in compile time.
Other errors are because of first.
You must specify the size of the arrays when you define Country on line 9.

You can't copy arrays using the assignment operator. In this situation you can use std::strcpy to do it. http://en.cppreference.com/w/cpp/string/byte/strcpy
Last edited on
Yup thanks for that :)
But how to make the user specifically enter a positive integer value in line 12?
I mean when I run the program, it still runs, even if the user enters any random value say "r" for "cin>>n;"
But the program doesn't ever work or end then. Why is that? And how to stress upon making the user enter only positive integer values? (The program does work when the user enters positive value but doesn't when the user deviates from it). I want to give an error in the Output screen as soon as the user enters something other than a positive integer. Or make the cout statement in line 11 appear again in output screen so that the user knows that every time the user enters something else and the program doesn't run further that something is wrong and that he/she has to enter the value of n again. Urgent!
Last edited on
how to make the user specifically enter a positive integer value in line 12?
1
2
3
4
5
6
7
int x = 0;
std::cout << "Enter positive integer: ";
while( !((std::cin >> x) && (x > 0)) ) {
    std::cout << "Wrong input, try again: ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Last edited on
What if the user enters an alphabet? And another question.. How can the user enter anything except an integer? Is that allowed? Because we define n as an integer data type and the user enters character data type..
Last edited on
What if the user enters an alphabet?
It asks him to re-enter.
How can the user enter anything except an integer?
If you try to extract non-integer data to integer, stream fail bit is set and nothing is extracted
In code while( !((std::cin >> x) && (x > 0)) ) {:
(std::cin >> x) Returns stream which then will be convertet to boolean value. You can read that as "return true if extraction operation was successful"
(x > 0)Second part of checking. Check if x is positive. Due to && operator short-circuiting, it will be checked only if extraction was successful.
Body of loop is just a cleanup: clearing stream state and removing all symbols from input buffer.
Last edited on
Hmm..thanks!
Just one more doubt. I get the following output while running the program:

Enter number of countries you want to enter: 2
Enter the countries:
Australia
Denmark
The countries you entered are:
Australia
Denmark

The countries you entered in descending alphabetical order are:
Denmark
Australia

Enter the name of the country you want to check: Denmark

The country you entered is not registered.

But it should show "The country you entered is registered."
What's the problem?
if(Country[i]==Input) You are checking pointers equality here.
Use strcmp() function or better use std::string type. C-strings is evil. Using them is like trying to shave with an axe: possible but dangerous.

Some info: "Hello" == "Hello" can result in both true (if compiler optimize string literals) of false (if it does not)
The following output comes:

Enter number of countries you want to enter: 5
Enter the countries:
Germany
France
Japan
India
Australia

The countries you entered are:
Germany
France
Japan
India
Australia

The countries you entered in descending alphabetical order are:
Japan
India
Germany
France
Australia

Enter the name of the country you want to check: Australia

The country you entered is not registered.


But it should say "The country you entered is registered."
Where is the problem?
Last edited on
In line 43
Yeah..sorry I did that.. the updated code is now up there. But still the wrong output comes as said in my previous reply..That's where I don't know what to do about that..
Reply please...
Please show us your updated code.

Also, 11 minutes is not a long time to wait for a reply. There's no need to bump.
strcmp returns 0 (interpreted as false) when strings are equal and -1 or 1 (interpreted as true) when first string is less or larger than second.
It is there..in the upper posts of the thread..I was saying that because I need this code for a school assignment. Seriously, its urgent.
Hmm MiiNiPaa..but line 46 is just checking if it is equal to 0 or not..means if Input matches one of the countries the user entered.
if input matches one of counties, strcmp returns 0 which is interpreted as false and count will not be increased. if input does not matches country strcmp returns 1 or -1 interpreted as true and count will be increased. Output your count in the end. It will be 4 in your case.
Oh yeah! Thanks for that! I should have written (!(strcmp(Country[i],Input))) which is equivalent to if(strcmp(Country[i],Input)==0)..instead of if(strcmp(Country[i],Input)) which is equivalent to if((strcmp(Country[i],Input))!=0)...My bad..nevermind I'll update that up in the code..thanks alot :)
Last edited on
It is there..in the upper posts of the thread..

Sorry - didn't see that.

I was saying that because I need this code for a school assignment. Seriously, its urgent.

Regardless, needlessly bumping your own thread is as likely to put people off helping you as it is to increase the speed at which you get help.

Couldn't you have used those 11 minutes to read the documentation on strcmp() to fix the mistake yourself? Or maybe to step through your code in a debugger to confirm where it was going wrong?
Pages: 12