Saving result to variable
Dec 9, 2016 at 6:10pm UTC
Hello,
So I'm just a newbie and I've been trying to write my own calculator (I've done it before but now I wanted it to be more complex and useful).
My question is: How can I save/write my results to a variable? I'm using cout to do calculations.
cout<<a<<" + " <<b<<" = " <<a+b;
Do I have to use classes/functions to save/write my results to variable? If yes then how?
Thank you.
Dec 9, 2016 at 6:25pm UTC
How did you assign values to a and b in your example?
Dec 9, 2016 at 6:30pm UTC
I assigned them with "cin". That example was just to illustrate how I wrote it in my previous program, I know the basics.
Dec 9, 2016 at 6:59pm UTC
Then I'm not sure I understand your question.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
int main( int argc, char ** argv )
{
int
a = 0, // Assignment
b = 0, // Assignment
total = 0; // Assignment
std::cout << "Enter a: " << std::endl;
std::cin >> a; // Assignment
std::cout << "Enter b: " << std::endl;
std::cin >> b; // Assignment
total = a + b; // Assignment
std::cout << "Total: " << total << std::endl;
}
Dec 9, 2016 at 7:04pm UTC
I'm trying to make something like "history of results". Maybe this will clear things up.
Dec 9, 2016 at 7:26pm UTC
Do you mean to save the calculations to a file?
Dec 9, 2016 at 7:49pm UTC
No, just to e.g. 5 variables. Something like 20 >> result (in batch) but to variable not file.
Dec 9, 2016 at 7:59pm UTC
How about an array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#define RESULTS_SIZE 5
int Results[ RESULTS_SIZE ];
for ( int i = 0; i < RESULTS_SIZE; i++ )
{
/* Add code to get variables: */
/* Calculate the result: */
/* Now assign the result to the array: */
Results[ i ] = total;
} /* for( i < RESULTS_SIZE ) */
Topic archived. No new replies allowed.