Variable output to exit message

Hello,

I'm still a bit new to c++ programming and though I've gotten a handle on the basic input/output functions and methods I'm still looking to learn about how to make a sort of random or multi-choice output generator to a simple calculator program I am trying to write. I will paste the program thus far in, I am just hoping someone might be able to give me advice or reference to how I might go about outputting a different string of feedback every time though. I don't just want to print the result, of the calculations, I want to print different messages for each time of either wrong input or successful calculation. I eventually want to turn this program into one that teaches step by step the core functions of mathematical calculations to any user...
Thank you in advance.

* Created on June 20, 2016, 5:24 PM
*/

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int Number;


int welcomeMessage();

void addNums(int, int);

void printResult(int);

string invalEntry();

int main(int argc, char** argv) {

welcomeMessage();
int num1,
num2;

do
{
cout << "Type two integers to find the sum: " << endl;
cin >> num1 >> num2;

if (num1 <= 0 && num2 >= 0)
{
int Number;
while( Number <= 10 )
++Number;

////cout << "Number " << Number << endl;
cout << "Invalid entry, please choose positive number: "
<< Number;



//return welcomeMessage();
}
else{
addNums(num1, num2);
}


}while(num1 <= 0 && num2 >= 0);
return 0;
}

int welcomeMessage()
{
cout << "Math operations, this is a lovely experience: " << endl;
}

void addNums(int x, int y)
{
printResult(x + y);
}

void printResult(int total)
{
cout << "Total: " << total << endl;
}
Last edited on
Hi,

for printing different messages you have to create a array of strings and use them

1
2
3
4
5
6
7
8
9
10
11
12
13
string *correct_ans[]= { 
  "\nAhhh.\n\n",

	  "\n WOW!\n\n",

	  "\n CORRECT ANSWER\n\n",

	  "\n Thats correct!\n\n",

	  "\n Good going!!!.\n\n",

	  "\n woooooah\n\n"
};


and when the user enters correct answer use random function

1
2
3
4
5
6
	
if(ans.compare(real_ans)==0)
{
srandom(time(0));
cout<<"\n"<< strings [random()%5] ;//as there are 5 possible output
}


this way if user enters correct answer she/he will be greeted with different stuff every time...


you can increase your database for greetings and all that stuff so that there's less repetition

I eventually want to turn this program into one that teaches step by step the core functions of mathematical calculations to any user...


that's great, check out https://www.wolframalpha.com/

https://github.com/mkroman/wolfram-alpha - this github will help you

Hope it helps
Last edited on
thank you 007. That was actually really helpful, and all of the code seems to be looking great. However I keep getting this error message now, that is related to trying to declare " ans ",

if(ans==real_ans) --- "Identifier whose declaration cannot be found."

however, everything I've tried doesn't seem to be working. I'm wondering if there is something else I need to do in order to make ans work properly so that it can test properly for the string pass.

I was wondering if you might know how to go about properly declaring ans.
whoops! my bad its string compare function

Edited my post accordingly
Topic archived. No new replies allowed.