indentifier was not being initialized - c++

was doing school project here and run into a problem
My program was support to be user guessing a number which is 47 and if they got it wrong they have 5 total of 5 tries. if still didnt get it the program will exit. If they got it there will be a message saying that how many tries it took for then to get the answer.
Therefore, i have to have a identifier for the number of tries it took. But C++ says that i didnt initialized the identifier. Please help
#include <iostream>
using namespace std;
int main()
{
int intSecretNum = 47;
int intGuess;
int GuessNum;
cout<< "Guess the secret number (between 0 and 100 inclusively): ";
cin>> intGuess;
while(intSecretNum != 47, GuessNum<= 5)
{
if( intGuess < intSecretNum)
cout << "Your Number is smaller than the secret number";
else if (intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
GuessNum++;
if(GuessNum>5)
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
cout<< "You have used "<<GuessNum <<" to guess te secret number which is " <<intSecretNum<<".";
cout<<"program terminated."<<endl;
return 0;
}
Please help =D

You forgot to initialize GuessNum:
int GuessNum = 0;
^^^
Additionally, the comma in the while condition does not do what you think it does, you mean && which is "and" in C++.

Related

My current code isn’t running properly in MS Visual Studio?

I am working on a project for the office today and I’m running into a bit of a stumble today on it. My code is properly debugged but it when ran it ignores all the parameters I set up and becomes smashed all together in a one line executable (if that makes sense). Here’s the prompt and what I have so far:
“A business needs to calculate the bonus points for sales representatives. The bonus points are based on how much each sales representative sold the year.
Write a program to prompt the user to enter the sales amount for a sales representative. Include different functions for each group of Sales representatives.
Display the bonus points as integers.
Please see the table showing the Groups, Sales, and Bonus points:
Groups Sales Bonus points:
A $0 - $100, 000 500 points
$100, 001 - $1,000,000 1, 500 points
B $1,000,001 - $2,000, 000 2,000 points
$2,000,001 - $3,000, 000 2, 500 points
C $3,000,001 - $4,000, 000 3, 000 points
$4,000,001 and over 5, 000 points
Input Validation: Do not accept negative numbers for sales.”
And here’s what I have tried so far:
#include <iostream>
#include <iomanip>
using namespace std;
float salesamounts; //sales made
int username; // Sales rep name
int main()
{
cout << “Please enter your sales made for the year: $”
cin >> salesamounts;
cout << endl;
// exception on program
if (salesamounts <= -1);
{
if (salesamounts <= -1);
cout << “Value cannot be negative. Please input again.”
}
// Group A placement
for (;salesamount <= 100000;)
{
if ((salesamounts <= 100000)
cout << “Congratulations! You earned 500 Bonus Points!”
break;
{
for (; salesamounts > 100001 < 1000000;)
{
if ((salesamounts > 100001 < 1000000));
cout << “Congrats! 10000 Bonus Points have been credited to you!”
break;
}
According to your requirement, you could use if-elseif-else to judge each group of Sales representatives, which is like:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double salesamounts; //sales made
int username; // Sales rep name
cout << "Please enter your sales made for the year : $" << endl;
while (cin >> salesamounts) {
// exception on program
if (salesamounts < 0.0) {
cout << "Value cannot be negative.Please input again." << endl;
}
//judge salesamounts
else if (salesamounts>0.0 && salesamounts <= 100000.0) {
cout << "Congratulations!You earned 500 Bonus Points!" << endl;
}
else if (salesamounts > 100000.0 && salesamounts <= 1000000.0) {
cout << "Congrats!1500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 1000000.0 && salesamounts <= 2000000.0) {
cout << "Congrats!2,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 2000000.0 && salesamounts <= 3000000.0) {
cout << "Congrats!2,500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 3000000.0 && salesamounts <= 4000000.0) {
cout << "Congrats!3,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 4000000.0 ) {
cout << "Congrats!5,000 Bonus Points have been credited to you!" << endl;
}
else {
break;
}
}
}
By the way, you could also use switch statement to do this.
You did the input part almost correctly, but after that you confused the if-else-if part with for loops.
When taking the user input, you can use a while loop to check whether the value is negative or not. This will make the program to prompt the user to enter a value again if the entered value does not match the criteria:
float salesamounts;
cout<<"Enter the sales amounts for the year:"; //the program will prompt the user initially
cin>>salesamounts; //user inputs the value here
while(salesamounts<0) //this line checks if the value is negative, if yes, it'll continue running this block till the value is non-negative
{
cout<<"Negative numbers not allowed. Please enter again: "; //Suppose the value is negative. Then the program will display this line and prompt the user to enter again
cin>>salesamounts; //user enters the value again
}
The next step in your program is to then check the user input and make the program act accordingly. The if-else statement will do that perfectly:
if (salesamount<=100000) //checks the value. If this is true, then then the following line will run. Else, the program will move to the next check.
cout<<"Bonus point is 500"; //displays the output if the above condition is correct
else if (salesamount<=1000000) //second check
cout<<"Bonus point is 1500"; //displays if the second condition is true
.
.
.
else
cout<<"Bonus point is 5000";
and so on.
Just to clarify, a loop is used only when you want a particular block of code to run repeatedly, whereas, to check or compare two values, you use the if-else statement.
Note that I've not written the second check as if (salesamounts>=100001 && salesamounts<=1000000) and also, I gave the last check as only else, not else if.

For Loops (C++)

Assignment:
The program should ask the user to enter a positive number and display all numbers from 1 to the input value. If the number is not positive, an error message should show up asking the user to re - enter the number.
My specific problem:
For my program, if the user enters an incorrect number and then re - enters a positive number, it does not display all the numbers from 1 to the input value. The program just ends.
#include <iostream>
using namespace std;
int main()
{
int userChoice;
int i = 1;
cout << "Enter a positive integer" << endl;
cin >> userChoice;
if (userChoice > 0)
{
for (i = 1; i <= userChoice; i++)
{
cout << "Loop 1:" << endl;
cout << i << endl;
}
}
else if (userChoice < 0)
cout << "Please re - enter" << endl;
cin >> userChoice;
system("pause");
return 0;
}
You need some sort of loop at the top of your program, that keeps asking for input until the user provides something valid. It looks like a homework assignment, so I will provide pseudo-code, not something exact:
std::cout << "Enter a number:\n";
std::cin >> choice;
while (choice wasn't valid) { // 1
tell the user something went wrong // 2
ask again for input in basically the same way as above // 3
}
// after this, go ahead with your for loop
It is actually possible to avoid the duplication here for step 3, but I worry that might be a little confusing for you, so one duplicated line really isn't such a big problem.
As an aside, you may wish to reconsider your use of what are often considered bad practices: using namespace std; and endl. (Disclaimer - these are opinions, not hard facts).

Random number generator for C++ popping up with an error?

I am having trouble with my number generator. Syntax wise, everything is working properly. I mainly wanted to use functions to see if they would work properly. When I run the program, a message pops up and says that my variable "guess" is not initialized. Can anyone give insight as to why this may be happening?
Also note that even though I didn't include my libraries in the code below, they are present in the actual program itself.
using namespace std;
int game();
string playAgain();
int main(){
game();
playAgain();
return 0;
}
int game(){
int guess;
int guessesTaken = 0;
int number = rand() % 10 + 1;
int count = 0;
cout << "I am thinking of a number between 1 and 10, can you guess it? " << endl;
while (guessesTaken < count){
cout << "Take a guess: " << endl;
cin >> guess;
if (guess > number)
cout << "Too High. Try again!" << endl;
if (guess < number)
cout << "Too Low! Try again!" << endl;
if (guess == number)
break;
}count++;
if (guess == number)
cout << "Congratulations!" << endl;
return 0;
}
string playAgain(){
string play;
cout << "Want to play again?: " << endl;
if (play == "y" || "Y")
main();
else
cout << "Thanks for playin" << endl;
return 0;
}
It happens because you did not initialise guess, precisely as the warning says.
Sure, if you assume that the cin >> guess operation will always succeed, then initialisation becomes largely irrelevant as the variable will take on a deterministic value.
But:
the compiler does not know this when it warns you, and
you have no error checking on your cin >> guess operation; not only can you not assume that it will always succeed, but your program has no clue whether it actually did.
Furthermore, the entire loop may not be executed at all if your other variables have the right values, so the compiler is completely right in its observation.
Initialise your variables and put error checking around your stream operations.
Furthermore, that count++ should obviously be in the loop body, and your loop otherwise needs refactoring in general because it begins with the 0 < 0 case. You should think hard about what you intend the semantics of your program to be.

Do-while loop issue: Try to develop a simple game

Well, I'm writing to make a dice game. I tried searching dice game here but none of it seems to answer my question. This isn't a problem about the dice roll thing anyway. It's about the do while loop. I am very new to this site, I just found out about this via Maximum PC Magazine so please bear with me. Also I am new to programming.
Here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(NULL));
int userRoll = rand() % 6 + 1 ;
int computerRoll = rand() % 6 + 1 ;
string yesOrNoChoice;
string commandToThrowDie;
do{
cout << "Please enter \"throw\" (lowercase) to roll the die: ";
cin >> commandToThrowDie;
} while(commandToThrowDie != "throw");
do{
cout << "You rolled: " << userRoll << endl
<< "The Computer rolled: " << computerRoll << endl;
if (userRoll < computerRoll){
cout << "You lose. Try again? [Yes/No]: ";
}
if (computerRoll < userRoll){
cout << "You win! Try again? [Yes/No]: ";
}
if (computerRoll == userRoll) {
cout << "It's a draw. Try again? [Yes/No]: ";
}
cin >> yesOrNoChoice;
} while(yesOrNoChoice != "Yes");
system ("pause");
return 0;
}
The problem is that after asking the user to enter a choice at the end of the do-while-loop the program exits loop no matter what I enter, instead of looping back to another throw of the die.
It ends up like this:
I copied your code and it compiled and ran perfectly. Doesn't make sense exactly, but no issues. I say it doesn't make sense since when "Yes" is entered that is what kills it. I believe what you want it while(yesOrNoChoice == "Yes"). Perhaps having it as != was making you think you were getting the wrong behavior? Also, you should be using if, else if, else statements, not just if.

C++ Guess Number game crashing on other computers and infinite loop fix

// Guess my number
// My first text based game
// Created by USDlades
// http://www.USDgamedev.zxq.net
#include <cstdlib>
#include <ctime>
#include <string>
#include <iostream>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); // seed the random number generator
int guess;
int secret = rand() % 100 + 1; // Generates a Random number between 1 and 100
int tries =0;
cout << "I am thinking of a number between 1 and 100, Can you figure it out?\n";
do
{
cout << "Enter a number between 1 and 100: ";
cin >> guess;
cout << endl;
tries++;
if (guess > secret)
{
cout << "Too High!\n\n ";
}
else if (guess < secret)
{
cout << "Too Low!\n\n ";
}
else
{
cout << "Congrats! you figured out the magic number in " <<
tries << " tries!\n";
}
} while (guess != secret);
cin.ignore();
cin.get();
return 0;
}
My code runs fine on my computer but when a friend of mine tries to run it, The program crashes. Does this have to do with my coding? Also I found that when I enter a letter for a guess, my game goes into an infinite loop. How can I go about fixing this issue?
The "crash" is probably related to missing runtime libraries, which would result in an error message similar to
The application failed to initialize
properly [...]
...requiring your friend to install the missing runtime libraries, e.g.
http://www.microsoft.com/downloads/en/details.aspx?familyid=a5c84275-3b97-4ab7-a40d-3802b2af5fc2&displaylang=en
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84
Choose the version that matches whatever version of Visual Studio you used to develop your application, as well as the target platform.
As for your application entering an infinite loop: after entering a letter, the input stream will be in an error state and thus unusable. Code similar to the following will prevent that:
#include <limits>
...
...
...
std::cout << "Enter a number between 1 and 100: ";
std::cin >> guess;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Basically, the code clears the error bits and removes any remaining input from the input buffer, leaving the stream in an usable state again.