Non-lvalue in assignment?
This program is supposed to take a boy or girl name and output its popularoty rank.
The text file is laid out as
1. Jacob Emily
2. David Alyssa
...
...
I cant figure out what I am doing wrong. Some suggested:
else if(boyRank !=0 && girlsRank = 0)
You probably want an == there, not an =.
but then is reports : expected primary-expression before '=' token
Code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char boys[1000][20], girls[1000][20], targetName[20], found;
int count = 0, boyRank, girlsRank, num;
cout<<"Enter the first name that you would like to find the \n";
cout<<"popularity of from baby names in 2004.\n";
cout<<"Be sure to capitalize the first letter of the name"<<endl;
cin >> targetName;
ifstream in;
in.open ("babynames2004.txt");
if (in.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
while(count < 1000 && in >> boys[count] >> girls[count])
{
count++;
}
for (int i = 0; i < 1000; i++)
{
if(strcmp(targetName, boys[i])==0)
{
found = true;
boyRank = i+1;
}
if(strcmp(targetName, girls[i])==0)
{
found=true;
girlsRank = i+1;
}
if(boyRank !=0 && girlsRank !=0)
{
cout << targetName <<" is ranked "<< boyRank <<"among boys.\n";
cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n";
}
else if(boyRank !=0 && girlsRank = 0)
{
cout << targetName <<" is ranked "<< boyRank <<"among boys.\n";
cout << targetName <<" is not ranked among to 1000 girl names.\n";
}
else if(boyRank = 0 && girlsRank != 0)
{
cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n";
cout << targetName <<" is not ranked among to 1000 boy names.\n";
}
}
return 0;
}
Works, but will not print name is it belongs to a girl and a boy.
Well I changed the code and now it works for the Boys name and Girls name. But now when i type in a name that is both a girls and boys name such as "Alex" it will only print the boys/girls name that comes first. So because Alex shows up on the boys list first, it stops there. If i remove the break statements, then it will print multiple statements.
for instance
Alex is ranked 63 among boys.
Alex is not ranked among top 1000 girl names.
...
...
...
Alex is ranked 63 among boys and 990 among girls.
...
...
...
but when i place break statements after each if statement, it chooses the "else if" statement, and not the if statement. I dont get it.
Code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
ifstream in;
in.open ("babynames2004.txt");
if (in.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
char boys[1000][20], girls[1000][20], targetName[20], found;
int count = 0, boyRank, girlsRank, num;
cout<<"Enter the first name that you would like to find the \n";
cout<<"popularity of from baby names in 2004.\n";
cout<<"Be sure to capitalize the first letter of the name"<<endl;
cin >> targetName;
while(! in.eof( ))
{
in >> num;
in >> boys[count];
in >> girls[count];
count++;
}
for (int i = 0; i < 1000; i++)
{
if(strcmp(targetName, boys[i])==0)
{
found=true;
boyRank = i+1;
}
if(strcmp(targetName, girls[i])==0)
{
found=true;
girlsRank = i+1;
}
if(boyRank !=0 && girlsRank !=0)
cout << targetName <<" is ranked "<< boyRank <<" among boys. and "<< girlsRank<< " among girls\n";
else if(boyRank !=0 && girlsRank == 0)
{
cout << targetName <<" is ranked "<< boyRank <<" among boys.\n";
cout << targetName <<" is not ranked among top 1000 girl names.\n";
}
else if(boyRank ==0 && girlsRank != 0)
{
cout << targetName <<" is ranked "<< girlsRank <<" among girls.\n";
cout << targetName <<" is not ranked among top 1000 boy names.\n";
}
}
system("pause");
return 0;
}