Can you help me guys? I'm a total beginner. My code worked fine then KEEP LOOPING FOREVER and never goes back to or cmd would crash with "Process terminated with status -1073741676". It should loop once then CIN >> again. It happens when I enter 10 digit numbers in my CIN >>.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class noteAssign { //This class return "A" if the random number generated is between 1 and 10
public:
int x;
int noteOut(int x){
if(x>1 && x<10){
cout << "ITS A" << endl;
return x;
}else{
cout << "IT'S NOT A" << endl;
return x;
}
}
}gonote;
int main()
{
cout << "Match the note's Hertz!" << endl;
cout << "Your answer may range from 1 to 20" << endl;
cout << "Type 0 to quit" << endl;
int noteIn; //No real purpose YET
do {
srand(time(0)); //ADDING MULTIPLE RAMDOMIZER FOR WIDER RANDOM RANGE
int rand1 = 1+(rand()%20); //randomizer 1
int rand2 = 1*(rand()%20); //randomizer 2
int hzout = (rand1 * rand2 + rand1 / rand2)%20; //rand 3
noteAssign gonote;
cout << gonote.noteOut(hzout) << endl; //calls the function and gives the parameter
cin >> noteIn; //No real purpose YET
} while(noteIn != 0); //program quits when you enter "0"
};
Related
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);
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.
The prompt is to start with a random number and to keep replacing that number repeatedly under the conditions that (1) if the number is even, you divide it by two and (2) if the number is odd, you multiply it by three then add one.
So, for example:
If the number was 13, then the output would be: 40 20 10 5 16 8 4 2 1
(Also the program must stop after the value of 1 is reached)
#include <iostream>
using namespace std;
int main()
{
int x;
int lengthcount=0;
cout << "Enter a number: ";
cin >> x;
while(x%2==0)
{
x=x/2;
cout << x << " ";
lengthcount++;
}
while(x%2==1)
{
x=x*3+1;
cout << x << " ";
lengthcount++;
}
if(x==1)
{
return 1;
}
cout << "Length:" << lengthcount << endl;
}
This is what I have so far. But when I compile and run the code only the first value of 40 appears. Not the rest of components. I'm assuming it has to do with the loops not connecting with one another. How do I get it so that the output of one loop would go to the other loop and back?
Two sequential loops aren't connected, and there's no way that you can or should make them so.
Instead, have a single loop, with an if/else inside it to handle the odd/even cases respectively.
The way the code currently is, neither the division by 2^n, nor the 3n+1 operation can be performed more than once. You need to have an outer loop around these operations.
The correct way to exit a loop is with the break keyword, not return.
int main()
{
int x;
int lengthcount=0;
cout << "Enter a number: ";
cin >> x;
while(true) {
while(x%2==0)
{
x=x/2;
cout << x << " ";
lengthcount++;
}
if(x==1)
{
break;
}
x=x*3+1;
cout << x << " ";
lengthcount++;
}
cout << "Length:" << lengthcount << endl;
}
How do I get it so that the output of one loop would go to the other loop and back?
You syould introduce loop in your code.
Furthermore, because 1%2==1 is true, x==1 should always be false after while(x%2==1).
An example of fixed code:
#include <iostream>
using namespace std;
int main()
{
int x;
int lengthcount=0;
cout << "Enter a number: ";
cin >> x;
for(;;)
{
while(x%2==0)
{
x=x/2;
cout << x << " ";
lengthcount++;
}
if(x==1) // check if the value of 1 is reached
{
break;
}
while(x%2==1)
{
x=x*3+1;
cout << x << " ";
lengthcount++;
}
}
if(x!=1) // x should be 1 in here
{
return 1;
}
cout << "Length:" << lengthcount << endl;
}
i have the next code which asks the user for a really long number like 100000000 and then it prints how many times a given digit appears on that number, the code works fine and does everything correctly, but the professor told me that i dont have to use strings or chars, but when the code asks the user for a number it necessarily needs a string and i don´t know how to modify it, i used the gmp library
#include <iostream>
#include <stdio.h>
#include <gmp.h>
#define MAX 40
using namespace std;
void searchDigit(FILE *fd);
int NewNumber();
int main()
{
FILE *fd;
int otherNumber;
string text;
mpz_t num;
do
{
if((fd = fopen("File.txt","w+"))!= NULL)
{
mpz_init(num);
cout << "Give me the number: " << endl;
cin >> text;
mpz_set_str(num,text.c_str(),10);
mpz_out_str(fd,10,num);
fclose(fd);
searchDigit(fd);
otherNumber = NewNumber();
}
else
cout << "Fail!!" << endl;
}while(otherNumber);
return 0;
}
void searchDigit(FILE *fd)
{
int car,continue = 1,r;
char answer,digit;
if((fd = fopen("File.txt","r"))!= NULL)
{
do
{
r = 0;
fseek(fd,0,SEEK_SET);
cout << "What digit do you want to search? " << endl;
cin >> digit;
while((car = fgetc(fd))!= EOF)
{
if(car == digit)
r++;
}
cout << "The digit x=" <<digit<< " appears " << r << " times" << endl;
cout << "Do you want to search any other digit? " << endl;
cin >> answer;
if(answer != 'S')
continue = 0;
}while(continue);
}
else
cout << "Fail!!" << endl;
}
int NewNumber()
{
char answer;
cout << "DO you wish to work with a new number? " << endl;
cin >> answer;
if(answer == 'S' || answer == 's')
return 1;
else
return 0;
}
Thanks in advance
Depends on how big your input might actually be... but for retrieving digits you could do something like:
#include <iostream>
using namespace std;
typedef unsigned long long UINT64;
int main() {
UINT64 i;
std::cin >> i;
while (i >= 1) {
int digit = i % 10;
std::cout << digit << " ";
i /= 10;
}
}
input: 18446744073709551614
outputs: 4 1 6 1 5 5 9 0 7 3 7 0 4 4 7 6 4 4 8 1
I'm working on a currency converter program that converts the old system of British pounds, Shillings, and pence, into their new system, which is a type of Decimal Pound. Where 100 pence equals a pound. Here is the code for the program
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int calcNum(int pound, int shilling, int pence)
{
pence = pound*240 + shilling*12 + pence;
return pence;
}
int calcNew(int total_pence, double dec_pound)
{
dec_pound = total_pence / 240;
return dec_pound;
}
int main()
{
int pence;
int shilling;
int pound;
const int OLD_POUND = 240;
const int OLD_SHILLING = 12;
double total_pence;
double dec_pound = 0;
double deci_pound;
cout << "Please Enter the Amount of old pounds: ";
cin >> pound;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old shillings: ";
cin >> shilling;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old pence: ";
cin >> pence;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);
cout << (5, "\n");
cout << "The total amount in decimal pounds is: ";
cout << setprecision(2) << "\x9c" << deci_pound;
_getch();
return 0;
}
When I run this program however, I'm having a bit of a problem. No matter what the number input is, it always says 0 pounds. Just to make sure that the setprecision function at the end wasn't interfering with the code, I had originally set a cout statement with a _getch() after the two functions to show how much deci_pound came out to be calculated to, and once again, it came out as zero. So my issue seems to be somewhere in the functions running the calculations. If someone could help me with this, I would really appreciate it.
Your calcNew(...) function returns an int, make it return a double. Right now it casts to int which involves stripping the decimals.
In your code, dec_pound is set equal to zero, and you're deci_pound = calcNew(dec_pound, total_pence), which divides 0 by 240 = 0.
The order of the parameters when you call both functions is wrong. Your functions are declared and implemented as:
int calcNum(int pound, int shilling, int pence);
int calcNew(int total_pence, double dec_pound);
And then you call them like this:
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);