Like Tree1Likes
  • 1 Post By jimblumberg

Non-lvalue in assignment?

This is a discussion on Non-lvalue in assignment? within the C++ Programming forums, part of the General Programming Boards category; This program is supposed to take a boy or girl name and output its popularoty rank. The text file is ...

  1. #1
    Def
    Def is offline
    Registered User
    Join Date
    May 2011
    Posts
    6

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    In the following line you should be using girlsRank == 0.
    Code:
    else if(boyRank !=0 && girlsRank = 0)
    ]
    The = is the assignment operator, the == is the comparison operator.

    Also if your file is laid out like:
    1. Jacob Emily
    2. David Alyssa
    Then the following is not reading the file correctly:
    Code:
    while(count < 1000 && in >> boys[count] >> girls[count])
    If you are going to use the extraction operator ( >> ) you should be reading at least 3 variables, the number, the first name, and the last name. The extraction operator stops processing at the white space characters. I would suggest that after you extract the data from the file you print it out to insure the data that you read is what you want.

    Jim
    Last edited by jimblumberg; 05-09-2011 at 09:29 PM. Reason: Fix code and quote tags.

  3. #3
    Def
    Def is offline
    Registered User
    Join Date
    May 2011
    Posts
    6

    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;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    In the following code:
    Code:
    for (int i = 0; i < 1000; i++)
    {
    You should replace the 1000 with count, since you only read in count entries, and any elements of the array that you don't read will be filled with garbage values since they have never been initialized.

    Also you may want to make the variable num an array since this should be the rank of the names.

    Next if you print out the names in the strcmp() if statements you should be able to print both the boys and girls ranking.

    Also you need to find an indentation style and use it, your program is almost impossible to read.

    Also if you have learned about structures, you should use one to hold the file information.

    Jim
    Salem likes this.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    You should use std::string, std::getline and std::vector. What you are doing is dangerous. Stop it at once.
    (Never use std::cin >> with a char array.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid lvalue in assignment ?
    By MSkiLLz in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2010, 09:55 AM
  2. invalid lvalue in assignment
    By ChoCo in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 01:06 PM
  3. :523: error: invalid lvalue in assignment
    By nasim751 in forum C Programming
    Replies: 10
    Last Post: 04-14-2008, 04:08 AM
  4. invalid lvalue in assignment
    By saipkjai in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2007, 03:19 PM
  5. invalid lvalue in assignment
    By antonis in forum C Programming
    Replies: 8
    Last Post: 10-09-2005, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21