Thread: please help with C++ program

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by anon View Post
    Is this the Do Others' Homework day?

    While you may find the assignment challenging enough to attempt it yourself, refrain from posting full solutions.

    Now OP will just pick and hand in one of the solutions and learn nothing.

    i know it's somewhat frowned upon, but from the dirth of gross errors, it seemed pretty likely to me that he was too lost to even understand how to ask questions educatedly.

    and i don't know about you, but i learn a lot more from the examples in the documentation than the verbose english discussions.

    if he doesn't learn anything, well then he'll fail then class anyway.

  2. #17
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by m37h0d View Post
    educatedly.
    Hrmmm...
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by dudeomanodude View Post
    Hrmmm...
    Proper form is either "edjumucated" or "edjucated-like". The latter can also be adorned with the adverb "real".

  4. #19
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    lol i was originally going to say "intelligently" but thought that sounded a bit harsh.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    i cant get it to store females ages over 21 in the variable adult_Females same for adultMale
    help!!! please!!


    Code:
    #include<iostream>
    
    int main()
    {
        int age, males=0, females=0, adult_Males=0, adult_Females=0, minors=0,minorage_Sum=0, code, count=0,max;
        
        
               while(count++<max && code !=0)
               {
                max=code+age;
                 std::cout<< "Please enter the gender code 1= Female, 2= Male, 0= Exit" <<std::endl;
                          std::cin>>code;
                 std::cout<< "Please enter person age" <<std::endl;   
                          std::cin>>age;
                          }
               if( age<21)
               {
                        males=males+1
                        females=females+1
                        
                         }
                                     
                         if(males, age>=21)
                         {
                           adult_Males++;
                                   
                         }
                                 
                             if(age>=21); 
                             {                     
                                        adult_Females++;
                             }           
                                        
                                                                     
                                      {
                                      std::cout<< "Statistics:" <<std::endl;
                                      std::cout<< "Number of Females over 21"<<" "<<adult_Females<<std::endl;
                                      std::cout<< "Number of Males over 21"<<" "<<adult_Males<<std::endl;
                                      std::cout<< "Total People"<<" "<<minors+adult_Males+adult_Females<<std::endl;
                                      }
                                      if (minors>0)
                                      {
    			minors=males+females;
                                                   std::cout<< "Average of Minors:"<<(float)minorage_Sum/(float)males+females<<std::endl;
                                                   }
                                                   system("PAUSE");
                                                   return 0;
                                                   
     }

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by c++manofyear View Post
    i cant get it to store females ages over 21 in the variable adult_Females same for adultMale
    I too, continue to try to solve the problem of finding and then keeping adult females. I try to put them in my closet but they keep breaking out and running off into the woods screaming "kidnapper!"

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    i thought as long as i dont reset the variable to 0 that it would continue to have input stored in it.

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To get a better response, learn to format your code properly.

    This is what it should look like (had to add two semicolon to make the formatter work properly):
    Code:
    #include<iostream>
    
    int main() {
        int age, males=0, females=0, adult_Males=0, adult_Females=0, minors=0,minorage_Sum=0, code, count=0,max;
    
    
        while (count++<max && code !=0) {
            max=code+age;
            std::cout<< "Please enter the gender code 1= Female, 2= Male, 0= Exit" <<std::endl;
            std::cin>>code;
            std::cout<< "Please enter person age" <<std::endl;
            std::cin>>age;
        }
        if ( age<21) {
            males=males+1;
            females=females+1;
    
        }
    
        if (males, age>=21) {
            adult_Males++;
    
        }
    
        if (age>=21);
        {
            adult_Females++;
        }
    
    
        {
            std::cout<< "Statistics:" <<std::endl;
            std::cout<< "Number of Females over 21"<<" "<<adult_Females<<std::endl;
            std::cout<< "Number of Males over 21"<<" "<<adult_Males<<std::endl;
            std::cout<< "Total People"<<" "<<minors+adult_Males+adult_Females<<std::endl;
        }
        if (minors>0) {
            minors=males+females;
            std::cout<< "Average of Minors:"<<(float)minorage_Sum/(float)males+females<<std::endl;
        }
        system("PAUSE");
        return 0;
    
    }
    Here's the warnings it produces:
    test.cpp(26) : warning C4390: ';' : empty controlled statement found; is this the intent?
    test.cpp(7) : warning C4700: uninitialized local variable 'max' used
    test.cpp(7) : warning C4700: uninitialized local variable 'code' used
    test.cpp(8) : warning C4700: uninitialized local variable 'age' used
    All are important. If you use uninitialized variables then your program could do anything.

    The largest problem is that processing input happens after the loop that gets input. All this while I've been entering data and the program doesn't do anything with it!
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    if(age,males)
    {
            adult_males++;
    }
    this is syntactically and logically wrong.

    your statements with max and count do not appear to be doing anything useful to you.


    you need to think through the structure of your program before you start writing it.

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by m37h0d View Post
    Code:
    if(age,males)
    {
            adult_males++;
    }
    this is syntactically and logically wrong.
    No, it's syntactically fine but logically wrong.

  11. #26
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    odd usage. never seen it before. i don't know why you'd ever want to do that instead of explicitly using a logical and.

  12. #27
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    anyhow, @ OP:

    you have a misconception about how boolean logic works.

    things like ints can be evaluated in if statements , but the expression is evaluated as true if the argument (the int you're passing in) is >0, and false otherwise.

    you seem to think the application intuitively knows that if code == 2, then males = true. it doesn't.

    the expression you entered there is equivalent to:
    Code:
    if(males>0 && age>=21)
    {
          adult_males++;
    }
    which is not what you want to do.

    your program needs to differentiate between which adult_ variable to increment based on the value of code

  13. #28
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's syntactically fine in the sense that it will compile, but it's not doing anything near what you want it to do. Using the comma like that will invoke the comma operator, which will evaluate the first term, then the second term, then return the second term, ignoring the result of the first (except side effects).

    You should be using combinations of || (or) and && (and) in your if statements.

    But the bigger problem is that the variable code, which reads the gender from the user is not actually interpreted that way by the program.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #29
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by m37h0d View Post
    odd usage. never seen it before. i don't know why you'd ever want to do that instead of explicitly using a logical and.
    It's not equivalent to logical AND, it just evaluates to the value of the rightmost operand.

  15. #30
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    so it's useless? no wonder i've never seen it used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM