Xcode C++ Errors preventing further output past srand() - c++

I am working on a program that generates two random numbers and an if statement that generates either a "+" for addition or a "-" for subtraction. I currently cannot check and see what my putput is so I can correct any mistakes because the program runs my opening "Welcome" statement then displays in blue parentheses (lldb) and the code stops there. I noticed next to my srand(time(0)) function that it turned green and says "thread 1: breakpoint 1.1" and under it reads "Implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int'". Is there a way to workaround these or get the errors to go away? My code is below. Any help or insight would be appreciated, thanks!
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
cout << "Welcome to the Math Tutor!" << endl;
int N1, N2;
int O = rand() % 2;
int Result;
int Answer;
srand(time(0));
if(O == 2)
{
cout << "+";
}
else
{
cout << "-";
}
N1 = 100 + rand() % 999;
N2 = 100 + rand() % 999;
Result = N1 + O + N2;
cout << setw(10) << N1 << endl;
cout << setw(10) << N2 << O << "\n";
cout << setw(10) << "------\n\n";
cout << "Enter your answer: ";
cin >> Answer;
if(Answer == Result)
{
cout << "You are correct!\n\n";
}
else
{
cout << "You are incorrect, the correct answer is: " << Result << "\n\n";
}
cin.ignore(1);
return 0;
}

time(0) returns a value of type time_t, which apparently is a long on your machine.
When you pass this long to srand(), expecting an unsigned int, not all values of a long will fit in an unsigned int. You can shoehorn it in by using a cast to tell the compiler that you don't care much about this.
srand(static_cast<unsigned int>(time(0)));
As you look for some, more or less, random numbers, the loss of precision is not important in this case.

Related

Generating Random Math Operators in C++

So I am creating a program where I have to create random problem sets that have random numbers and operators. I had no problem making random numbers. However, I am confused on how to randomize the three operators I need to use (addition, subtraction, and multiplication). I know I have to use numbers to represent these three operators, but I don't understand how to do that. I have to use the random number generator in order to do this and If & Then statements. Here is my source code.
I've tried creating a separate constant called "const int MAXOP_VALUE = 3" . I am stuck on what to do afterward. How do I represent the addition, subtraction and multiplication operators as numbers?
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
/*Constants*/
const int MIN_VALUE = 1;
const int MAX_VALUE = 100;
/*Variables*/
int number_1;
int number_2;
int math_op;
/*Get the System Time*/
unsigned seed = time(0);
/*Seed the Random Number Generator*/
srand(seed);
/*Generates Random Numbers for the Math Problems*/
number_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
number_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
/*Answer to Problem*/
/*Explains How the Program Works*/
cout << "****************************************" << endl << endl;
cout << "Welcome to the awesome math tutor! \n";
cout << "Get ready to add, subtract, and multiply!" << endl << endl;
cout << "****************************************" << endl << endl;
cout << "How much is" << number_1 << math_op << number_2 << "?" <<
endl;
return 0;
}
I expect the output to be along the lines of this:
"What is 25 +42 ?"
"What is 54*3 ?"
"What is 76-2 ?"
One liner for generating random math_op. Remove the int math_op and put this line somewhere after srand(seed).
char math_op = "+-*"[rand() % 3];
And you may use switch-case statement for the actual calcuation.
Once you have the two random numbers, you can use another random number to generate the operation and expected result, something like:
char op; int expected;
switch(rand() % 3) {
case 0: op = '+'; expected = num1 + num2; break;
case 1: op = '-'; expected = num1 - num2; break;
default: op = '*'; expected = num1 * num2; break;
}
Then you'll be able to output the expression and compare what's entered with the expected result:
int answer;
std::cout << "What is " << num1 << " " << op << " " << num2 << "? ";
std::cin >> answer;
std::cout << "Your answer is " << (answer == expected) ? "right" : "wrong" << ".\n";
Normally I'd also suggest you check the expected result is okay, as in no overflow or divide-by-zero, or be wary of doing integer division where 5 / 2 == 2.
But with both numbers between one and a hundred, and divide-by-zero/integral-division being a non-issue as your specifications only allow for addition, subtraction, and multiplication, it should be fine.

C++ Issue with my code [duplicate]

This question already has answers here:
Why is cout printing twice when I use getline?
(2 answers)
Closed 5 years ago.
When I run this and after I select my number as a player, Computer returns me two outputs (instead of one...). I have no idea why, could you please help me explain why that happens?
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int random(int a, int b)
{
int num = a + rand() % (b + 1 - a);
return num;
}
int main()
{
srand(time(NULL));
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
int min = 1;
int max = 100;
int comp;
string player;
while(1) {
comp = random(min, max);
cout << "Computer: " << comp << endl; // why does this get called twice??
getline(cin, player);
if (player == "too high") {
max = comp - 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "too low") {
min = comp + 1;
cout << "min: " << min << " max: " << max << endl;
} else if (player == "correct") {
cout << "Computer found the number..." << endl;
break;
}
}
}
It's because you are mixing input using >> and getline. getline reads to the next newline, >> does not. After you have entered your number, there is still a newline left behind, you have typed it, but hasn't yet been read. The first time you call getline that left behind newline gets read, and the program doesn't pause. Only on the second time that you call getline does your program pause and wait for you to type something.
Simple way to fix the problem is
int myNum;
cout << "Choose your number, human: ";
cin >> myNum;
// flush pending newline
string dummy;
getline(cin, dummy);

Why aren't my if else codes working?

I am making a simple random number guesser game (example below), and it works perfectly until your guess is higher than the number. when it happens, it prints: "My number is lower than thatMy number is higher than that" When it is just supposed to print "My number is lower than that". Also, when you guess the right answer, it prints "My number is higher than that"
My code is:
#include <iostream>
#include <unistd.h>
#include <cstdlib>
int main() {
int highnum;
int guess;
srand(time(0));
std::cout << "Pick a number greater than 2";
std::cin >> highnum;
int randynumeber = (rand () % highnum) + 1;
std::cout << "I have picked a random number between 1 and " << highnum << " which you need to try and guess";
int guesses = 0;
int stop = 0;
while (guess != randynumeber)
{
if (stop == 0)
{
sleep(0.1);
stop = 1;
std::cout << std::endl << "Guess a number";
std::cin >> guess;
if(guess>randynumeber)
std::cout << "My number is lower than that";
else if(guess<randynumeber)
std::cout << "My number is higher than that";
stop = 0;
}
}
std::cout << std::endl << "guesses = " << guesses;
std::cout << "You win!";
}
You are not testing the condition on the else statement. You need to test it otherwise it simply executes ( guess < randynumeber); - which gives a logic answer and then executes the next piece of code which is the next cout statement.

C++ - I´m coding my first program, but I ran into some trouble

I have started to learn C++ lately and wanted to make my first "game"/program. I have run into some difficulties.
My errors so far are as follows:
(rand()%a) -> changing "a" doesn´t do anything (for example if the generated number is 2 and "a" is 1 the generated number stays 2).
The following code does not work:
while(!(b = c)){
cout << "Enter your guess! \n";
cin >> c;
if(c<b){
cout << "Bigger! \n";
}
if(c>b){
cout << "Smaller! \n";
}
d++;
}
My complete program is as follows:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Max. Limit
int a;
// Random Number
int b;
// Guess
int c;
// Tries counter
int d = 0;
cout << "Enter highest possible number, setting the max. limit for the program. \n";
cin >> a;
srand(time(0));
b = 0 + (rand()%a);
if(b =! 1){
c = 1;
}
if(b = 1){
c = 2;
}
while(!(b = c)){
cout << "Enter your guess! \n";
cin >> c;
if(c<b){
cout << "Bigger! \n";
}
if(c>b){
cout << "Smaller! \n";
}
d++;
}
if(b=c){
cout << "Congratulations! You have guessed was right! The number was indeed " << b << " !" << endl;
cout << "You needed " << d << " tries to find the number! \n";
}
return 0;
}
Alright, the first thing you need to know is (as drescherjm already pointed out), b = c is not what you want here. Instead, you want b == c for comparison.
Another thing is:
if(b =! 1){
c = 1;
}
if(b = 1){
c = 2;
}
You can avoid initializing c to a different value than b by replacing your while-loop with a do-while-loop. If you then also get rid of using namespace std; and use <random> instead of rand(), rename your short variables (a, b) to what they are actually doing, you're code becomes clearer and more modern.
#include <iostream>
#include <random>
int main()
{
int max_limit{ 0 };
int random_number{ 0 };
int guess{ 0 };
int number_of_guesses{ 0 };
std::cout << "Enter highest possible number, setting the max. limit for the program. \n";
std::cin >> max_limit;
std::random_device now;
std::mt19937 engine(now()); //random seed
std::uniform_int_distribution<int> r(0, max_limit); //range
random_number = r(engine);
do{
std::cout << "Enter your guess! \n";
std::cin >> guess;
if (guess<random_number){
std::cout << "Bigger! \n";
}
if (guess>random_number){
std::cout << "Smaller! \n";
}
number_of_guesses++;
} while (random_number != guess); //do the code above until this is false
std::cout << "Congratulations! Your guess was right! The number was indeed " << random_number << " !" << std::endl;
std::cout << "You needed " << number_of_guesses << " tries to find the number! \n";
return 0;
}
Example run:
Enter highest possible number, setting the max. limit for the program.
100
Enter your guess!
50
Smaller!
Enter your guess!
25
Smaller!
Enter your guess!
10
Bigger!
Enter your guess!
15
Congratulations! Your guess was right! The number was indeed 15 !
You needed 4 tries to find the number!
So yeah, that's working.
a = b assigns a the value of b
a == b compares de values of both variables.
About first problem, check about seed.

my code crashes when introducing giant numbers

i have the next code which asks the user to introduce a number larger than 100000000, and then it asks for a digit that the code must search in the number, finally the code shows how many times the digit appears on the number, it seems to be easy but i have a restriction:
the data type cannot be a string or a char, thats why i am using an int, but when i introduce a real big number like 100100010000100 the code just doesn´t work properly, how could i solve this, any ideas???if someone could help me out with this i would appreciate it a lot
#include <iostream>
using namespace std;
int searchDigit(long int num,int digit);
int main()
{
long int num;
int digit,x;
cout << "Give me the number: " << endl;
cin >> num;
cout << "Digit " << endl;
cin >> digito;
x = searchDigit(num,digit);
cout << "\nThe digit " << digit << " appears " << x << " times" << endl;
return 0;
}
int searchDigit(long int num,int digit)
{
int r,c,p = 0;
for(c = num;c != 0;c = c/10)
{
r = c % 10;
if(r == digit)
p++;
}
return p;
}
Notice that in searchDigit you have c = num in the for-loop. If long int is larger than int - which is true on many platforms - you will lose the high bits of num.
If you enabled more compiler warnings, this would probably be picked up. It's hard to be more specific, since you haven't provided information about the platform, compiler, or the flags used.