Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed why this code give me inncorrect answer..??

+9 votes
asked May 7, 2021 by Yousef Nino (190 points)
closed Sep 28, 2021 by Admin
#include <iostream>

using namespace std;

int main()

{

int a ;

int b ;

int c ;

//the rule of the operation

c = a + (2*b) ;

// the code

cout<< "enter the value of (a) number \n " ;

cin>> a ;

cout<< "enter the value of (b) number \n " ;

cin>> b ;

cout<< "the value of (c) nwmber equal"<< c<<"\n" ;

}
closed with the note: answered

12 Answers

+4 votes
answered May 15, 2021 by xDELLx (10,500 points)
Shouldnt you be computing c after you have vales of a,b from user?

What you have done is computed value of c before asking user for values of a,b.

Move the computation of c before you print the c vlaue> :)
0 votes
answered May 17, 2021 by Hamsavani Ramachandra Navale (150 points)
the statement c= a+ (2*b) even before the variables a and b have their values read results in ,undefined values in a and b and operating on that ,results in an incorrect value.
0 votes
answered May 18, 2021 by Vikas Kumar (190 points)
This is giving you an incorrect answer because before asking the value of "a" and "b" from the user, you have written "c = a + (2*b) ;". You know that varible without any value will be always 0. In this program, operation has first taken place and "c" variable has been stored then you asking the user to store the value of "a" and "b", so operation answer ("c" variable value) would always be 0 + (2*0)  = 0 + 0 = 0.
0 votes
answered May 20, 2021 by C KOUSHIK (140 points)
You need the declare or initialize the variables before computing the result of expression.
0 votes
answered May 20, 2021 by Yashwanth Bittu (160 points) 1 flag

 before computing c ,you must read a and b values.

commented May 21, 2021 by Peter Minarik (84,720 points)
edited May 21, 2021 by Peter Minarik
Someone has provided an answer days ago.

What's the point repost his answer again?
commented May 28, 2021 by Shrijan Dwivedi (100 points)
because the question you have asked is on the top of frequently asked questions list.
0 votes
answered May 21, 2021 by Shubham Chaudhary (140 points)
after declaring variables a,b just write the input lines of both then u have to give the mathetical statement
0 votes
answered May 21, 2021 by SanketSarafOfficial (140 points)
The execution of program happens line by line so write the C part after the cin part means at the last
0 votes
answered May 26, 2021 by Satyam Pandey (140 points)
#include <iostream>
using namespace std;
int main()
{
    
int a ;

int b ;

int c ;

cout<< "enter the value of (a) number \n " ;

cin>> a ;

cout<< "enter the value of (b) number \n " ;

cin>> b ;

c = a + (2*b) ;

cout<< "the value of (c) number equal to: " <<c<<"\n" ;

}
0 votes
answered May 29, 2021 by Gnyanasree B S (150 points)
#include <iostream>

using namespace std;

int main()
{
int a,b,c;
cout<< "enter the value of (a) number \n " ;
cin>> a ;
cout<< "enter the value of (b) number \n " ;
cin>> b ;
c = a+(2*b);
cout<< "the value of (c) number equal\t"<< c <<endl ;

}

Try compiling this .

 U must accept values of input a and b first and then compute c
0 votes
answered May 30, 2021 by Vikash Roy (140 points)
c is being calculated using garbage value, also calculation of c is before reading the value of a and b.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...