While loop string problem

This is a discussion on While loop string problem within the C++ Programming forums, part of the General Programming Boards category; ok so im new at computer programming and im trying to run this program. yes im takeing a class and ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    While loop string problem

    ok so im new at computer programming and im trying to run this program.
    yes im takeing a class and this is a project.
    I dont need to have the part i need help on done. its just for extra credit but i have no idea why its not working.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
        int Students, Student_Age, Total_Age, female, male;
        float NumberOFstudents, Average_Age;
        string gender;
        
        cout << "Enter the number of students in the class\n";
        cin >> Students;
        while (Students<=0)
        {
              cout << "Please enter a correct value. Try again.\n";
              cin >> Students;
        }
        female=0; male=0;
        Total_Age=0;
        NumberOFstudents=Students;
        while (Students > 0)
        {
              cout << "Enter age of studnet\n";
              cin >> Student_Age;
              while (Student_Age<=0)
              {
                    cout << "Age has to be greater than 0 try again\n";
                    cin >> Student_Age;
              }
              cout << "Enter gender of student with f or m.\n";
              cin >> gender;
              while ( ((gender != "f") || (gender != "F")) || ((gender != "m") || (gender != "M")) )
              {
                   cout << "Please enter a correct value.\n";
                   cout << "Remeber (f for girl) (m for guy)\n";
                   cin >> gender;
              } // this while loop is the part that i cant get to work right. it just keeps looping
              // over and over again without working.
              if((gender == "f")||(gender == "F"))
              {
                   female++;
              }
              else if((gender == "m")||(gender == "M"))
              {
                   male++;
              }   
              Total_Age+=Student_Age;
              Students--;
        }
        Average_Age=(Total_Age/NumberOFstudents);
        
        if (Average_Age>=100)
        {
              cout << setprecision(4);
              cout << "Average age of students = " << Average_Age << endl;
        }
        else if (Average_Age>=10)
        {
             cout << setprecision(3);
             cout << "Average age of students = " << Average_Age << endl;
        }
        else if (Average_Age>=0)
        {
             cout << setprecision(2);
             cout << "Average age of students = " << Average_Age << endl;
        }
        
        cout << "Total girl "<< female << endl;
        cout << "Total guys "<< male << endl;
        cin.get();
        cin.get();                    
        return 0;
    }
    yes i realize that its quite a long and cumberosome(well long to me) code. and a bit messy but

    I could have used char for memory but whatever i trust strings more.
    and yes i could use a goto loop. but hey what would be the fun in that.
    Any suggestions??
    I got this far by myself but well i cant figure out why its not working.

    it should work. ex ( ((true) || (false)) || (( false ) || ( false )) ) it should be true shouldnt it?
    well
    any ideas. im probably missing something small
    Last edited by Makachu; 09-10-2007 at 06:45 PM. Reason: hmm

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Makachu View Post
    any ideas. im probably missing something small
    Yeah.

    Think about it.

    Code:
    if(x != 'f' || x != 'F')
      ....
    x can only have a single value. Therefore, if x == 'f', then it follows that x != 'F'. And vice versa. Therefore, this condition is ALWAYS true. You need to use AND not OR.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    I kind of get it but im still a little confused

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are two ways to express what you're trying to do. You want the loop to run if the letter given by the user is invalid, right? So you could say: if the gender is not "f" and the gender is not "F" and the gender is not "m" and the gender is not "M" then it is invalid. You could also say: if it is not true that: the gender is "f" or the gender is "F" or the gender is "m" or the gender is "M", then it is invalid.

    Now, the code you wrote says: if the gender is not "f" or the gender is not "F" or the gender is not "m" or the gender is not "M", then it is invalid. See the difference? Your version was wrong. Just because the gender is not "F" doesn't mean it isn't valid, because it could be "M". In that case it is not "F" but it is still ok.

    Write a truth table for each version and plug in different values to see what you get.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Makachu View Post
    I kind of get it but im still a little confused
    Maybe it's clearer if we use a different example condition:

    "If you are under 21 years old, OR, if you are over 21 years old..."

    Obviously, this applies to ALL people.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I could have used char for memory but whatever i trust strings more.
    and yes i could use a goto loop. but hey what would be the fun in that.
    Don't apologize for correct choices.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ string problem
    By gatewalker in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2009, 11:40 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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