You are here:

C++/help fix c++ code

Advertisement


Question
QUESTION: #include<iostream.h>
void main()//begining of program
{
  int steelbars;//declaring variables
  int steelbars1;
  int steelbars2;
  int steelbars3;
  int steelbars4;
  int steelbars5;
  int steelbars6;

  cout<<"Enter number of steel bars :";//request user to input
  cin>>steelbars;



  {
     if (steelbars<20) {steelbars1=steelbars*1000;} //if statement
        cout<<steelbars1<<"=is the cost for less than 20 steelbars"<<endl;
        if (steelbars >21 && steelbars <40) {steelbars2=steelbars*950;}
        cout<<steelbars2<<"=is the cost for 21-40 bars"<<endl;
        if (steelbars>41 && steelbars<60) {steelbars3=steelbars*900;}
        cout<<steelbars3<<"=is the cost for 41 to 60 bars"<<endl;
        if (steelbars>61 && steelbars<80) {steelbars4=steelbars*850;}
        cout<<steelbars4<<"=is the cost to 61 to 80 steel bars"<<endl;
        if (steelbars>81 && steelbars<100) {steelbars5=steelbars*800;}
        cout<<steelbars5<<"=is the cost of 81 to 100 steel bars"<<endl;
        if (steelbars >100) {steelbars6=steelbars*750;}
        cout<<steelbars6<<"=is the cost of more than 100 steel bars"<<endl;
        
  }
}
..

i want it to display ony one amount on the output

ANSWER: Please,

carefully study If-else statements in C++ and you will get this problem..as a starter, I will just correct one of your code here to help you get inspired to research it..:

if (steelbars<20) {cout<<steelbars1<<"=is the cost for less than 20 steelbars"<<endl;} //if statement

else if (steelbars >21 && steelbars <40) {cout<<steelbars2<<"=is the cost for 21-40 bars"<<endl;}

else if (steelbars>41 && steelbars<60) {cout<<steelbars3<<"=is the cost for 41 to 60 bars"<<endl;}

else if (steelbars>61 && steelbars<80) {cout<<steelbars4<<"=is the cost to 61 to 80 steel bars"<<endl;}

else if (steelbars>81 && steelbars<100) {cout<<steelbars5<<"=is the cost of 81 to 100 steel bars"<<endl;}

else //if steelbars >100)
cout<<steelbars6<<"=is the cost of more than 100 steel bars"<<endl;

}
.....study the structure rather than the correctness for now..

---------- FOLLOW-UP ----------

QUESTION: when i use else if statement i get this error:
C:\Users\NoisyBoy\Desktop\fd.cpp(17) : error C2181: illegal else without matching if
.. thats y i used if only ,, i know about the else if statement ... so whats causing that error

Answer
Hello,

The syntax is correct, maybe there is something you are doing wrong in your code but the syntax I gave you previously is the right one.

It goes like this :

if(i==0)
{
cout<<"0"<<endl;
}
else if(i==2)
{
cout<<"2"<<endl;
}

else cout<<"1"<<endl;


it is as simple as this... check your full code and be sure they are in line..

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry

Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.