problem in exchanging values

#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 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
1st add
using namespace std;

before the int main()

then give me a few more minutes
actually that should do it
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 ===|

this error occur.
you have to type std::cin... cin is only available in the standard namespace.
So best way to do it, is just type std::cin, std::cout etc...
..didnt work..

more errors..

see..

error: no match for operators std::cin std::basic_stream<_CharT, _Traits>:: operator>>

OMG.. i'm losing hope.
Last edited on
this error is a problem in all of the source code i made today...
cant figure out whats wrong..
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;
}
Last edited on
uhm..
tnx..
sorry for my stupidity.. i'Ll try my best to learn this.. harhar..
Topic archived. No new replies allowed.