#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;
elseif(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!
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!
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..
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.
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)
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?
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..
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 :)
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?