I wrote this code, and I'm having a little problem with it. The point of the code is for the user to enter a desired number, then that number gets subtracted by 45 then dived by the percentages and all that dived by 2.

Also I dont know how to switch the percantage. For example I want the user to type in a percentage for the weight to be divided by. But instead the user to type in .5 as 50%, I want the user to type in 50, 60 or 70. And the program knows to conver that into .5, .6 or.7 as percents.

Also the loop is not working to well. The ( cout<< "Invalid response " << endl; ) line comes in everytime I say yes and its not suposed to come in. Its only suposed to come in if I enter a different word besides yes or no. But it comes everytime I enter yes. And if I enter a diferent word besides yes or no the program goes crazie.

Whats going on here?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int weight;
    int barr = 45;
    double percentage;
    
         string response;
         bool good = false;
         while (!good)
    {
      
    cout << "Enter the weight you want calculated."<< endl; 
    cin >> weight;
    cout << "At what percentage do you want to callculate?" << endl;
    cin >> percentage;
    cout << "Your weight of " << weight << " at " << percentage 
         << " % " << " = ";
                

               
               if ( percentage == 50 )                
               cout << ( ( (weight - barr ) * percentage ) /2 ) << endl;
        
               else if  ( percentage == 60 )
               cout << ( ( (weight - barr ) * percentage ) /2 ) << endl;
        
               else if  ( percentage == 70 )
               cout <<  ( ( (weight - barr ) * percentage ) /2 ) << endl;

               else if  ( percentage == 80 )
               cout <<  ( ( (weight - barr ) * percentage ) /2 ) << endl;

               else if  ( percentage == 90 )
               cout << ( ( (weight - barr ) * percentage ) /2 ) << endl;
        
        
    cout<< "Do you want to enter a different percentage? Yes | No  "  ;
    cin >> response;
        
        
            if (response == "Yes" || response == "yes " || response == "No"
                                  || response == "no" )
        {
                good = false;
        }
            else
        {
                cout<< "Invalid response   " << endl;
        }
               
            if (response == " No" || response == "no" )
        {
                    break;
        }
   } 

    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 11 Replies

>>Also I dont know how to switch the percantage
How would you do this with pencil and paper. If you buy a pencil for $1.00 and the taxes are 6 percent what it the total price? Hint: 1.00 plue 1.00 * 0.06. The same in your problem. If you enter 50, then the percentage is 50/100 or 0.5.

The formula (untested) should be something like this:

float number
float percentage

float answer = ((number-45.0) * (percentage/100)) / 2.0;

Damn did not think of that. I was over thinking a simple line. What about the Loop? And why is that Invalid Respons keep showing up?

apparently the value of response is something else -- maybe you typed "YES" (all upper-case). The best way to handle that is to convert response to all upper case letters so that you don't have so may variations of the same string. That way all you have to do is compare with "YES" or "NO".

Ok then why does it make the program flip when I type in anything else besides yes or no?

Is flip some technical term in computers I've never run across? You need to use words that actually describe the symptoms.

maybe flip is like giving someone the bird :mrgreen:

Ha haha you guys are helarious. Flip as in flip out. But I did not write the out.
If I type in anything besides yes or no the program goes into an endless loop.

works ok for me -- here's the code I changed

#include <algorithm>
<snip>

      cout<< "Do you want to enter a different percentage? Yes | No  "  ;
      cin >> response;
     // convert to all upper-case        
      transform(response.begin(),response.end(),response.begin(),toupper);        
       if (response == "YES" || response == "NO")
       {
                good = false;
       }
       else
       {
                cout<< "Invalid response   " << endl;
        }
               
        if (response == "NO")
        {
                    break;
        }

I get an error:

`transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'

if (response == "Yes" || response == "yes "|| response == "No"                                || response == "no" )

You added a space after the lowercase "yes", which I believe may be the cause of your original invalid response.

Since your program was looking for "yes ", "yes" is an invalid response, and you have hopefully gained a new respect and loathing for the blasted typo.

I get an error:

did you include algorithm header file ? what compiler are you using ? some compilers may need the :: global scope operator in front of toupper function name transform(response.begin(),response.end(),response.begin(), ::toupper); If that doesn't fix it then you may need to repost your current code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.