Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.31/5 (4 votes)
See more:
C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int sumFunc(int num1, int num2);
char askYesNo(string question);
int main() {
    srand(static_cast<unsigned int="">(time(0)));
    int ans;
    addition = sumFunc(int num1, int num2);
    cin>>ans;
    
    if (ans == addition) {
        cout<<"Correct! "<<ans<<"is the="" answer!";
="" tryagain="askYesNo("Do" you="" wish="" to="" continue?");
="" if="" (tryagain="=" "y")="" {
="" addition;
="" }
="" else="" break;
="" cout<<"wrong!"<<ans<<"="" is="" not="" answer."<<endl;
="" cout<<addition<<"="" answer.";
="" }

="" return="" 0;
}

int="" sumfunc(int="" num1,="" int="" num2)="" randomnumber="rand();
" num1="(randomNumber" %="" 99)="" +="" 1;
="" num2="(randomNumber" sum="num1" num2;
="" do="" cout<<num1<<"="" "<<num2<<"="?";
" 
="" }while="" (num1="">= 0 && num2 >= 0);
    
    return num1 + num2;
}

char askYesNo(string question) {
    char response;
    
    do {
        cout<<question<<"(y n)="" ?";
="" cin="">>response;
    }while (response != "y" && response != "n");
    
    return response;
}
 
 
}


What I have tried:

I've tried defining num1&2 as random number in their parameter parentheses.
But all things considered I have no idea what to even try.
Posted
Updated 21-Feb-24 8:02am
v2
Comments
0x01AA 21-Feb-24 14:18pm    
Looks like something went wrong when you pasted your code? e.g. all these '=""'
Dave Kreskowiak 21-Feb-24 15:14pm    
You also never described what's wrong.
0x01AA 21-Feb-24 15:31pm    
As you know, of course, as usual, that is the job of the volunteers :-)
Rick York 21-Feb-24 15:47pm    
You really need to edit your post and re-paste your code. Also, be sure to select all and then click untabify because I think the tabs are what fouled it up. I wrote re-paste because there are several things missing that would prohibit what you have pasted from even compiling. For example, the function sumfunc does not have an opening brace. Also, in the if statement about tryagain the word addition appears with a semicolon. That is a meaningless statement so either something is missing or it is just an error.
0x01AA 21-Feb-24 15:57pm    
And can you please confirm, same is also in original version and not caused by my addition of 'code tag'...
I think it is like this, that the original is same wrong. I only like to be sure that my edit of the question was not wrong ;)

When you call the rand function, it returns a single random number: it doesn't make the variable you assign that to change it's value every time you use it.
Unfortunately, your code is so corrupt that I can't work out what exactly you typed: I can't get anything that will compile out of that.

But ... if you want two random numbers, you need to call rand twice:
C
num1 = rand();
num2 = rand();
Secondly, the numbers are (pseudo) random: that can be any value in the range 0 to RAND_MAX (which in practice will be a minimum of 32,767) so to get a specific range you need to use the Modulus operator on both returned values, not just one.
Thirdly, if you want numbers between 0 and 99 inclusive, you need to use x % (upperValue + 1): if you sue x % 99 the values will be between 0 and 98 inclusive, not 0 and 99!
See here: Modulo Operator (%) in C/C++ with Examples - GeeksforGeeks[^]
 
Share this answer
 
Rephrasing your requirement as "function that generates sum of two random numbers between 0 and 99", I would write

C++
int sum_of_rand()
{
  int a1 = rand() % 100;
  int a2 = rand() % 100;
  return (a1+a2);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900