Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am making a basic vending machine without object orientation and need a menu system for some reason even when i enter 2 into menuChoice the first if condition is met and it prints 2Here's your choco bar sir" when i want a Museli bar!!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Choclate=0;
int Museli=0;
int CheesePuffs;
int Apple;
int Popcorn;
int menuChoice = 0;
while (menuChoice != -1)
{
cout << "-ENTER CORRESPONDING NUMBER-" << endl;
cout << "1. Milk Choclate Bar" << endl;
cout << "2. Museli Bar" << endl;
cout << "3. Cheese Puffs" << endl;
cout << "4. Apple" << endl;
cout << "5. Popcorn" << endl;
cout << "Enter Choice: ";
cin >> menuChoice;
if (menuChoice = 1)
{
Choclate = Choclate + 1;
cout << "Here's your choco bar sir." << endl;
}
else if (menuChoice = 2)
{
Museli = Museli + 1;
cout << "Here's your museli bar sir." << endl;
}
}
}
You need to use ==
i.e.
if (menuChoice == 1)
Better still look up switch
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
So in this small project for my school I needed to build a word guessing game everything works fine except the string university which always gives output incorrect, I tried changing it to universit and it works but university doesn't work. I cant figure out what the problem is.
Also are there any other alternatives to strcmp()?
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{ // at first we are basically going to give an intro to the gameseco
cout << " HANGMAN : GUESS THE WORD AND NOT DIE!!\n\n\n";
cout << " Welcome to the game player!!\n\n";
cout << " Rules: Guess the word by typing a letter based on given hints.\n";
cout << " Multiple incorrect guesses will HANG YOU.\n\n";
// here we add different words in the game for the guessing system using a 2 dimensional array
char word[20][20] = {
"higher",
"secondary",
"first",
"year",
"cotton",
"university",
"computer",
"science",
"project",
"hangman",
};
char newword[20], guess[10];
bool again = 1;
while (again == 1)
{
again = 0;
srand(time(0));
int x = 0, wrong = 0, z = (rand() % 10) + 0;
strcpy(newword, word[z]); // so we used strcpy command to copy an element of the 2D array of randomly generated index to a new string
cout << "Enter your guess. \nHINT: Word Length(indicated by underscores) = "; // hint no. one the gives us the exact lenth of word
for (x = 0; newword[x] != '\0'; x++)
{
cout << "-";
}
cout << "\nWord starts with: '" << newword[0] << "'"; // hint no. two which gives the player an idea what the word might be
cout << "\n\n";
for (wrong = 1; wrong <= 6; wrong++) // the loop here checks whether the input given by the user is correct or not
{
cin >> guess; // the input is taken here
if (strcmp(guess, newword) == 0) // the input is compared with the word to be guessed
{ // using the strcmp() function from the string.h library
cout << "Correct!\n"; // if the guess is correct the program will print correct and correct and end
break; // correct guess will terminate the loop
}
else if (wrong == 1)
{
cout << "Opps! Wrong guess!\n\n"; // if player inputs wrong word a poll will appear for hanging the person
cout << "I=-=\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 2)
{
cout << "Opps! Wrong guess again!\n\n"; // each wrong word will result in the appearance of of a hanging person
cout << "I=-=\n";
cout << "| |\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 3)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 4)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 5)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "| o\n";
cout << "I\n";
}
else if (wrong == 6)
{
cout << "Opps! Wrong guess again!\nLast Chance!!\n\n"; // unfortunately the player couldn't guess the word
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n"; // the hanging person compeletely appears and the program ends
cout << "|--|--\n";
cout << "| o\n";
cout << "I | |\n";
cout << "YOU ARE DEAD HAHAHA!!!";
}
}
cout << "Do you want to play again?\nPress 1 for yes, 0 for no."; // here it is asked if we want to play again
cin >> again;
}
return 0;
}
The problem seems to be this declaration:
char guess[10];
Your array declaration should always cater for a \0 character at the end of string. The length of "university" is 10 character, to hold the entire string you required an array of 11 characters.
So please change the length of guess array to be longest string length plus 1.
char guess[11];
As a side note:
Since you are using C++, you should fully utilize the facilities offers by the language. In C++, you should use std::string to store array of characters, use std::vector to store a collection of objects etc.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
Hi I'm trying to use a cin input but on my second while loop, the inputs are not running though correctly. How would I go about this?
Here's what I have:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using std::endl;
using std::cout;
using std::cin;
using namespace std;
int a, A;
int main() {
int choice, num;
cout << "State Search" << endl;
cout << "" << endl;
cout << "1. Enter the first letter of your desired state" << endl;
cout << "2. Press 2 to Quit" << endl;
cout << "" << endl;
bool done = false;
while(!done) {
//user inputs desired choice
cin >> choice;
if(choice == a || choice == A)
{
cout << "Which State? (enter a number)" << endl;
cout << "1. Alabama" << endl;
cout << "2. Alaska" << endl;
cout << "3. Arizona" << endl;
cout << "" << endl;
while (!(cin >> num))
{
if(num == 1)
{
cout << "test output 1" << endl;
}
else if (num == 2)
{
cout << "test output 2" << endl;
}
else if (num == 3)
{
cout << "test output 3" << endl;
}
} break;
} break;
}
return 0;
}
Once the user chooses one of the above states using a number I want to display the chosen output.
Here is a cleaned up version of what you are trying to do with some inline comment where you were going wrong.
// You only need to include iostream in your example program
// Only include what you need
#include <iostream>
#include <limits>
// You do not need to include all of the std namespace in the local scope
// if you are using the things you want.
using std::endl;
using std::cout;
using std::cin;
using std::streamsize;
int main()
{
cout << "State Search\n\n";
cout << "1. Enter the first letter of your desired state\n";
cout << "2. Press 2 to Quit\n" << endl;
// Removed all the while(!done) / break; as it is just noise in the current iteration of your program.
// if you use an int for the choice variable, you will actually try to parse the input
// as an integer and return 0 if the user inputs 'a'
// you need to use a char for what you want to do
char choice;
cin >> choice;
// Also your a and A int value do not make sense as they are init to 0
// (global initialization), I guess what you want to do is compare to the char 'a' or 'A'. You could also transform the input to lowercase and compare only to 'a'.
if (choice == 'a' || choice == 'A')
{
// Use \n not std::endl as it will flush the output buffer
// all the time. std::endl is a false friend, use it only
// when you want to present your text to the user.
cout << "Which State? (enter a number)\n";
cout << "1. Alabama\n";
cout << "2. Alaska\n";
cout << "3. Arizona\n" << endl;
// We want to wait until we get a correct input and _then_
// do something with our input.
int num;
while (!(cin >> num))
{
// You might want to say something to your user if he doesn't input a correct number here.
// This will succeed if the user inputs 4, so you might also want to handle that case differently and ask the user to retry too in that case.
// The stream is now in error, you need to reset the state of the stream by clearing the error and emptying the buffer.
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
// Just showing another way of doing the if / else if dance in C/C++.
switch(num)
{
case 1: cout << "You chose Alabama" << endl; break;
case 2: cout << "You chose Alaska" << endl; break;
case 3: cout << "You chose Arizona" << endl; break;
default: cout << "I do not recognize the State" << endl; break;
}
}
// if we didn't receive 'a' or 'A' we just quit the program.
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have had problems with my program, for some reason Visual Studio shows the following message when I compile:
'opuntia::menu': non-standard syntax; use '&' to create a pointer to the member
I've seen that this error is related to omitting some (), but I think this is not the case. Any ideas to fix this problem?
opuntia.h
#pragma once
class opuntia{
public:
int op;
void menu();
};
opuntia.cpp
#include "opuntia.h"
#include <iostream>
using namespace std;
void opuntia::menu() {
do {
cout << "\n \t OPUNTIA: CUIDADO DE AGAVÁCEAS, CACTÁCEAS Y CRASULÁCEAS" << endl;
cout << "\t Registra tu planta y planea sus cuidados \n" << endl;
cout << "\t 1. Registro" << endl;
cout << "\t 2. Registro manual" << endl;
cout << "\t 3. Miscelánea" << endl;
cout << "\t 0. Salir \n" << endl;
cin >> op;
cin.ignore();
switch (op) {
case 1:
cout << "Registro" << endl;
break;
case 2:
cout << "Registro manual" << endl;
break;
case 3:
cout << "Miscelánea" << endl;
break;
case 0:
cout << "Goodbye" << endl;
break;
default:
cout << "Elige una opción válida" << endl;
break;
}
} while (menu != 0);
}
main.cpp
#include <iostream>
#include <locale.h>
#include "opuntia.h"
using namespace std;
int main() {
setlocale(LC_CTYPE, "Spanish");
opuntia ejecutar;
ejecutar.menu();
return 0;
}
Within the member function menu in the condition of the do-while statement
do {
//...
} while (menu != 0);
you are using the name of the member function menu. It seems you mean
do {
//...
} while (op != 0);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So, I have recently started learning C++ (I am in first year college). I have been stuck with this error for a while now and none of my peers have been able to assist me in fixing this issue.
The error I am receiving is error: expected '{' before else
Here is the code:
switch (selectOption)
{
case 1:
cout << "Enter amount of people" << endl;
cin >> amountPpl;
//validating that there is more than 0 people
if (amountPpl > 0)
{
total = amountPpl * 30;
cout << "Your total for " << amountPpl << " is R" << total << endl;
}
else
{
cout << "You can't enter 0 people" << endl;
}
break ;
case 2:
cout << "Enter amount of Students" << endl;
cin >> amountPpl;
//validating input
if (amountPpl > 0)
{
total = amountPpl * 25;
cout << "The amount for " << amountPpl << "students is R" << total << endl;
}
else
{
cout << "You can't enter 0 people" << endl;
}
break;
case 3:
cout << "Are There any additional children? Answer with y/n" << endl;
cin >> decision;
//validating input
if (decision == 'y')
{
cout << "Please enter amount of additional children" << endl;
cin >> extraKids;
if (extraKids > 0)
{
total = 75 + (extraKids * 15 );
cout << "Your total is R" << total << "For the family package" << endl;
}
else
{
cout << "You cant enter 0 kids" << endl;
}
else // ***ERROR*** is here
{
cout << "Your total is R75";
}
}
break;
default:
cout << "invalid input, you can only select a number from 1-3 ";
}
link to an image
The error is in this section:
if (extraKids > 0)
{
total = 75 + (extraKids * 15 );
cout << "Your total is R" << total << "For the family package" << endl;
}
else
{
cout << "You cant enter 0 kids" << endl;
}
else // ***ERROR*** is here
{
cout << "Your total is R75";
}
#MikeCAT is correct. You cannot have two else sections. The first else should become an else if. The condition is obvious from the output string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i'm having an issue with my C++ code:
#include <iostream>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
void displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints) // Call this function to display
{ // ennemy related informations
cout << endl << "Ennemy health points: " << ennemyHealthPoints;
cout << endl << "Ennemy attack points: " << ennemyAttackPoints << endl;
}
int main()
{
//Player related variables
int healthPointsMax(100);
int actionPointsMax(100);
int recoveryPoints(100);
int healthPoints(100);
int actionPoints(100);
int attackPoints(100)
//Player related variables
//Ennemy related variables
int ennemyHealthPoints(230);
int ennemyAttackPoints(10);
//Ennemy related variables
//Main variables
string stringInput;
//Main variables
//TEXT
cout << "HP: " << healthPoints << endl;
cout << "AP: " << actionPoints << endl;
cout << "RP: " << recoveryPoints << endl;
cout << endl;
cout << "HP = Health Points, AP = Action Points, RP = Recovery Points" << endl;
cout << endl;
cout << "CONTROLS:" << endl;
cout << "attack [ennemy name] //attacks the ennemy" << endl;
cout << "heal [playername] // heals the selected player" << endl;
cout << endl;
cout << "A wild nugget appears!" << endl;
cout << endl;
cout << "What do you want to do?" << endl;
// TEXT
getline(cin, stringInput);
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);
}
return 0;
}
The debugger says that there is a problem with the fonction "void displayEnnemyStatus"
I checked every variable, there is no problem with that.
Am I doing something wrong?
Try :-
if (stringInput == "attack ennemy")
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
**displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints)**;
}
Instead of :-
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);