int main() {
int fval; // for first value
int sval; //for second value
int sum;
std::cout << " Enter first value: ";
std::cout << "Enter second value:" ;
cin >> fval = fval + sval; // Get the sum of two numbers and assign it to fval
cin >> sval = fval-sval; // fval (sum) - sval would give you original value of a and assign it to b.
std::cout << "The second value is:";
cin >> fval = fval - sval; // fval (sum) - sval(currently holds the orinial value of fval) would give you sval and assign it to fval.
std::cout << "The first value is:";
return 0;
}
..need help don't know what to do.. kindly help on fixing the errors...
tnx
ahm.. yeah,, i beliv this is right but when i tryd 2 execute it..
C:\Documents and Settings\earth10\Desktop\try\try lang.cpp||In function 'int main()':|
C:\Documents and Settings\earth10\Desktop\try\try lang.cpp|15|error: 'cin' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
Because all your codes are a mess, they try to input AND assign a value, in which the to assign value is complete bull crap. Your code is here, and I explained what it does and where it goes wrong below of it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include <tchar.h>
int main() {
int fval; // for first value
int sval; //for second value
int sum;
std::cout << " Enter first value: ";
std::cout << "Enter second value:" ;
cin >> fval = fval + sval; // Get the sum of two numbers and assign it to fval
cin >> sval = fval-sval; // fval (sum) - sval would give you original value of a and assign it to b.
std::cout << "The second value is:";
cin >> fval = fval - sval; // fval (sum) - sval(currently holds the original value of fval) would give you sval and assign it to fval.
std::cout << "The first value is:";
return 0;
}
When you execute this code it will go like so:
Int main() starts
Variables get created.
Enter first value gets printed
With no pause, Enter second value is printed behind it.
You then try to get user input to fval via cin, and assign a value to it in the same time. I suggest you look at the following page: http://www.cplusplus.com/doc/tutorial/basic_io/
Because your current code is hopeless.
Edit:
Gave it a try.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
int main()
{
int fval; // for first value
int sval; //for second value
int temp; // temporary value
std::cout << "Enter first value: ";
std::cin >> fval;
std::cout << "Enter second value:" ;
std::cin >> sval;
temp = fval; // Temporarily store fval
fval = sval; // Change the value of fval to that of sval
sval = temp; // To complete the swap we need to store the value that fval first held
std::cout << "The swapped first value is: " << fval << std::endl;
std::cout << "The swapped second value is: " << sval << std::endl;
return 0;
}