Hi, i'm trying to creating a program on c++ which relates to weight

this is my problem so far

cout << "Enter weight between 1 to 60KG" << endl;
       cin >> weight;
       
    
       if (weight >= 1 && weight <= 60)
         cout << "Your weight is accepted" << endl;
       if (weight < 1 || weight > 60)
          cout << "Please enter weight between 1 to 60KG" << endl;

when i enter a number greater then 60 it still says weight accepted. i don't know why this is happening when it is suppose to say "please enter weight between 1 to 60kg"

Could you post the complete code? The error is not apparent from what you have posted.

Try "else if" for the 2nd if condition !!!!

Try "else if" for the 2nd if condition !!!!

doesnt work, it comes up with some error msg saying before else


and heres the full code

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << "Enter weight between 1 to 60KG" << endl;
       cin >> weight;
 
 
       if (weight >= 1 && weight <= 60)
         cout << "Your weight is accepted" << endl;
       if (weight < 1 || weight > 60)
          cout << "Please enter weight between 1 to 60KG" << endl;

doesnt work, it comes up with some error msg saying before else


and heres the full code

Can't be the full code, where is weight declared? Paste your exact code, don't try to paraphrase.

Can't be the full code, where is weight declared? Paste your exact code, don't try to paraphrase.

// This program will calculate the charges for


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    
    char choice, weight;

    cout << "Welcome to the DOWHILE FREIGHT ordering cart" << endl;
    cout << "To begin ordering enter '1'" << endl;
    cout << "To quit enter '2'" << endl;
    
    cin >> choice;
    
    if (choice == '1')
    {
       cout << "Please Enter the weight of your item" << endl;
       cout << "We ship items between 1 to 60KG" << endl;
       
       cin >> weight;
       
    
       if (weight >= 1 && weight <= 60)
          cout << "We are able to ship that amount of weight" << endl;
          cout << "Please Enter the shipping number zone you would like to use" << endl;
          cout << "We have 4 Shipping zones, 1 being standard shipping, 2 Special standard shipping , 3 Express Shipping, 4 Express Special Shipping" << endl;
    
       if (weight < 1 || weight > 60)
          cout << "Please enter weight between 1 to 60KG" << endl;
       
          
    }
    
    else
        cout << "You must enter 1 or 2" << endl;
        
system ("pause");
return 0;
}

weight should be an integer and not a char. You were comparing the ASCII value of the first character (so if you typed in 70 you'd only get the 7 as a character variable holds exactly one character, and 7 has an ASCII value of 55 which is less than 60).

weight should be an integer and not a char. You were comparing the ASCII value of the first character (so if you typed in 70 you'd only get the 7 as a character variable holds exactly one character, and 7 has an ASCII value of 55 which is less than 60).

how do i change it to an integer

How did you declare it as a char? Change it from being a char to an int.

How did you declare it as a char? Change it from being a char to an int.

char choice, int weight;

like that?

its not working =/

Also: curly brackets! They're more important then you think.

This:

if (something) 
   dostuff();
   doOtherStuff();

is VERY different from:

if (something) {
   dostuff();
   doOtherStuff();
}
commented: Mad curly brace props +4

Also: curly brackets! They're more important then you think.

We're working our way up to that.

@OP: No, you can only use the comma for variables that are of the same type.

char choice;
int weight;

We're working our way up to that.

@OP: No, you can only use the comma for variables that are of the same type.

char choice;
int weight;

thanks mate

but when i still enter a number like 61 it still passes through

Put the curly braces like Nick Evan was saying. If you don't have the braces only the statement immediately below the if statement is associated with it. Put braces around lines 28 to 30. Now that whole grouping is associated with the top if statement.

Put the curly braces like Nick Evan was saying. If you don't have the braces only the statement immediately below the if statement is associated with it. Put braces around lines 28 to 30. Now that whole grouping is associated with the top if statement.

OMGGG thank you jonsca & Nick Evans GEEEESS!!

sorry i'm a beginner with c++ haha

but thank you!

dude ...try declaring int weight in a different line ...... then use "else if" ..

he could have managed without braces ...only thing required was "else if" .........

commented: wrong -2

OMGGG thank you jonsca & Nick Evans GEEEESS!!

sorry i'm a beginner with c++ haha

but thank you!

yeah i got another program man, when i type in >60 it brings up you have selected shipping zone2 but it says please enter weight between 1 to 60kg

i dont know why its coming up with this message


plus i dont know how to make a quit option and for some reason my loop at the main menu is not working, when i type in any number besides 1 or 2 it comes up with the message " please enter 1 or 2 " and it would loop back

heres the code

// This program will calculate the charges for

#include <iostream>
#include <iomanip>

using namespace std;
int main()

{   
    char choice;    // Choices in main menu.
    int weight;  // Weight to be entered.
    int shippingzone;    // Shipping Zone to be selected
    
    // Make a choice in the main menu
    cout << "Welcome to the DOWHILE FREIGHT ordering cart" << endl;
    cout << "To begin ordering enter '1'" << endl;
    cout << "To quit enter '2'" << endl;
    cin >> choice;
    
    if (choice == '1'){
               cout << "Please Enter the weight of your item" << endl; // Enter the weight of your item
               cout << "We ship items between 1 to 60KG" << endl;
               }
               cin >> weight;
               if (weight >= 1 && weight <= 60){
                          cout << "We are able to ship that amount of weight" << endl;
                          cout << "Please Enter the shipping number zone you would like to use" << endl;   // Choose which shipping zone you want to use
                          cout << "We have 4 Shipping zones, 1 being standard shipping, 2 Special standard shipping , 3 Express Shipping, 4 Express Special Shipping" << endl;
                          cin >> shippingzone;
                          }
                          if (shippingzone >= 1 && shippingzone <= 4){
                                           cout << "you have selected shipping zone" << shippingzone << endl;
                                           }
                          if (shippingzone < 1 || shippingzone > 4){
                                           cout << "Please enter shipping zone between 1 to 4" << endl;
                          }
               
               if(weight < 1 || weight > 60){
                         cout << "Please enter weight between 1 to 60KG" << endl;
                         cin >> weight;
                         }
           

             
    while(choice < 1 || choice > 2) 
    {
               cout << "Please Enter either 1 or 2" << endl;
               cin >> choice;
               }    
        
system ("pause");
return 0;
}

It's curious that you named this program the "dowhile" freight cart. I think a few do/while loops will help you here. Wrap one around the meat of your code like from 13 to 50 and have the loop exit if the user selected 2 (you can leave your one if statement in there since if the response is 1 it's going to do all the processing). Then, each time you are re-prompting the user for an invalid response, use a do/while instead of the if statement.

As far as the other problems, do a quick "desk check" of your program by hand and make sure all the groupings are ok for the if statements.

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.