Having issues with my code not working. I'm trying to get it to exit the do while by pressing any key on the keyboard but without hitting enter. My code snippet is below.
void LemonadeBuyANDCheck()
{
int iCupsToBuy = rand() % (MAX_CUPS - MIN_CUPS) + MIN_CUPS;
bool continueLoop = true;
do
{
int Userinput = 1;
Userinput = !_kbhit();
fflush(stdin);
if (Userinput == 1)
{
continueLoop = false;
}
StoreIsOpen();
int randomNumber = rand() % 100;
if (iCupsOfLemonadeInStock == 0)
{
cout << "Customer came to buy some lemonade but you didnt have any in stock :(\n";
//continueLoop = false;
}
else if (randomNumber <= 25)
{
cout << "Wow your lemonade is way too expensive!\n";
iCustomerCounter++;
}
else if (randomNumber >= 75)
{
cout << "Eww your recipe for lemonade... Wow..\n";
iCustomerCounter++;
}
else if (iCupsOfLemonadeInStock >= iCupsToBuy)
{
cout << "You sold " << iCupsToBuy << "cups of lemonade!\n";
iCupsOfLemonadeInStock = iCupsOfLemonadeInStock - iCupsToBuy;
iCupsOfLemonadeSold = iCupsOfLemonadeSold - iCupsToBuy;
//Money
fMoneyEarned += (iCupsToBuy * fCurrentLemonadePrice);
fCurrentMoney += (iCupsToBuy * fCurrentLemonadePrice);
iCustomerCounter++;
}
else if (iCupsOfLemonadeInStock < iCupsToBuy)
{
cout << "You didnt have enough lemonade in stock so you only sold " << iCupsToBuy - iCupsOfLemonadeInStock << endl;
fMoneyEarned += (iCupsOfLemonadeInStock * fCurrentLemonadePrice);
fCurrentMoney += (iCupsOfLemonadeInStock * fCurrentLemonadePrice);
iCupsOfLemonadeInStock = 0;
iCustomerCounter++;
}
} while (continueLoop != true);
fflush(stdin);
GameMenuSelectionVerifier();
}
I think you can use a function named "kbhit", it's in the "conio.h" header file. You can click this for details. And you can modify it like this:
while(!kbhit());
Related
I found an RPS game and wanted to improve on it by creating a scoring system but the //int compwin = compwin + 1; keeps giving me errors. I am still fairly new to coding in C++ and have no clue where the problem is standing so thanks for helping in advance. Also this code was taken from somewhere this is not my code but I wanted to try and improve on it.
#include <iostream>
#include <cstdlib>
#include <limits>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int choice;
int i;
int y;
int Y;
int comp;
char res;
int compwin = 0;
int choicewin = 0;
unsigned seed;
while (1==1) {
//The choices
cout << "Game Choices.\n\n";
cout << "1.Rock\n";
cout << "2.Paper\n";
cout << "3.Scissors\n";
cout << "4.Quit, exits the game.\n\n";
cout << "Please enter your choice.\n\n";
cin >> choice;
//-----------------------Player Imputs-----------------------------------
if (choice == 1) //Rock
{
cout << "You picked Rock.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 2) //Paper
{
cout << "You picked Paper.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 3) //Scissors
{
cout << "You picked Scissors.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 4)
{
return 0;
}
else if (choice != 1 || 2 || 3 || 4) // Debug
{
cout << "Uhhhh thats not one of the following.\n\nGoodbye!\n\n";
system("pause");
return 0;
}
//-------------------------Computer Choice-------------------------------
seed = time(0);
srand(seed); //RNG TIME
comp = rand() % 3 + 1; //Computer picks
if (comp == 1) //Computer rock
{
res = 1;
cout << "Rock!\n\n";
}
else if (comp == 2) //Computer paper
{
res = 2;
cout << "Paper!\n\n";
}
else if (comp == 3) // Computer scissors
{
res = 3;
cout << "Scissors!\n\n";
}
//-----------------------Victory Conditions------------------------------
if (comp == 1 && choice == 1) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 1 && choice == 3) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else if (comp == 2 && choice == 2) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 2 && choice == 1) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else if (comp == 2 && choice == 3) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 2 && choice == 2) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else {
std::cout << "Congrats! You won!" << endl;
//int choicewin = choicewin + 1; This is where the problem occurs
}
cout << "Heres the score, computer =" << compwin << "and player =" << choicewin;
cout << "Want to go again? (y/n)";
cin >> res;
system("cls");
}
while (res == y || Y);
system("pause");
return 0;
}
This
int compwin = compwin + 1;
is a declaration and initialization. A rather weird one, and to discuss why it is allowed would take us on a too big detour. So lets look at a simpler example:
int compwin = 1; // declares compwin and initializes it with 1
int compwin = 2; // compiler error, because compwin is already declared
You can only declare the same variable once (again the long complete truth is more involved, search for "shadowing" in case you care). You also only need to initialize a variable only once.
If you want to assign something to an already declared variable, you use assignment, as in
int compwin = 1; // declare and initialize
compwin = 2; // assign
Further note that
else if (choice != 1 || 2 || 3 || 4) // Debug
is not doing what you expect. The correct way is
else if (choice != 1 && choice != 2 && choice != 3 && choice != 4)
The mistake is a combination of ignoring De Morgan's Law and a wrong combination of several conditions. The operator|| expects a bool on both sides and unfortunately numbers happily convert to true (only 0 becomes false), but a good compiler may spit out warnings on that. Similar mistake is on while (res == y || Y);.
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 6 years ago.
Improve this question
here is my main . I get the error for the part I am checking whether the game had a winner or not. the errors are referring to the if parts where i wanna check the returned data from chkwin method
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
int main()
{
string choice = "";
string name1 = "";
string name2 = "";
string change = "";
int choice_num;
TicTacToe game;
cout << "Welcome to TicTacToe World!\n"
<< "In order to Start Please Enter the name of the first player\n\n";
getline(cin, name1);
cout << "\nGreat, Now Please Enter the name of the second player\n\n";
getline(cin,
name2);
cout << "\nAwesome Let's Get Started!\n";
do
{
cout << "\nPlease choose what do you want to do by entering the number of your choice\n"
<< "1.Start the game.\n"
<< "2.Change the names.\n"
<< "3.View Scores\n"
<< "4.Exit the Game :(\n\n";
getline(cin, choice);
if (choice == "1")
{
for (int i = 1; 1 <= 9; i++)
{
if (i % 2 != 0)
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice_num;
cin.ignore();
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
}
else
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice;
cin.ignore();
game.setMove(choice_num, 2);
if ( 1 == game.chkWin)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout << "The Game is a Draw! ";
}
}
}
}
else if (choice == "2")
{
do
{
cout << "\nwhich player do you want its name to be changed? (Enter 1 for the first, 2 for the second and 3 for both)\n";
getline(cin, change);
if (change == "1")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
}
else if (change == "2")
{
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else if (change == "3")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else
{
cout << "\nPlease Enter a Valid Choice.\n";
}
} while (change != "1" && change != "2" && change != "3");
}
else if (choice == "3")
{
cout << "\n" << game.getResults(name1, name2);
}
else if (choice != "4")
{
cout << "\nPlease Enter a Correct number\n";
}
} while (choice!= "4");
cout << "\nFinal Results are: \n\n"
<< game.getResults(name1, name2);
cout << "\nThank you for using our program. Hope to see You Again. Bye Bye!!\n";
system("Pause");
return 0;
}
my class
#include<iostream>
#include<string>
using namespace std;
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
private:
const static int SIZE = 3;
int table[SIZE][SIZE];
int results[SIZE];
int winner = 2;
public:
TicTacToe();
void setMove(int , int );
int chkWin();
string getResults(string , string );
};
#endif
here is my methods declaration:
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe()
{
for (int i = 0; i < SIZE; i++)
{
results[i] = 0;
for (int j = 0; j < SIZE; j++)
{
table[i][j] = 0;
}
}
}
void TicTacToe::setMove(int place, int player)
{
int field = place;
if (field == 1)
{
if (table[0][0] == 0)
{
table[0][0] = player;
}
else
{
cout << "\nYou cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 2)
{
if (table[0][1] == 0)
{
table[0][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 3)
{
if (table[0][2] == 0)
{
table[0][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 4)
{
if (table[1][0] == 0)
{
table[1][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 5)
{
if (table[1][1] == 0)
{
table[1][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 6)
{
if (table[1][2] == 0)
{
table[1][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 7)
{
if (table[2][0] == 0)
{
table[2][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 8)
{
if (table[2][1] == 0)
{
table[2][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 9)
{
if (table[2][2] == 0)
{
table[2][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
}
int TicTacToe::chkWin()
{
for (int i = 0; i < SIZE; i++)
{
if (table[i][0] == table[i][1] && table[i][0] == table[i][2])
{
if (table[i][0] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[i][0] == 2)
{
results[2] += 1;
winner = -1;
}
}
else if (table[0][i] == table[1][i] && table[0][i] == table[2][i])
{
if (table[0][i] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[0][i] == 2)
{
results[2] += 1;
winner = -1;
}
}
}
if ((table[0][0] == table[1][1] && table[0][0] == table[2][2]) || (table[0][2] == table[1][1] && table[0][2] == table[2][0]))
{
if (table[1][1] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[1][1] == 2)
{
results[2] += 1;
winner = -1;
}
}
else
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if(table[i][j] == 0)
{
winner = 2;
}
}
}
results[1] += 1;;
return winner;
}
}
string TicTacToe::getResults(string name1, string name2)
{
return ( name1 + " : " + to_string(results[0]) + "\n"
+ "Draws : " + to_string(results[1]) + "\n"
+ name2 + " : " + to_string(results[2]) + "\n");
}
Compareing functions with integers doesn't make sense. You have to use () operator to call functions like this:
if (game.chkWin() == 1)
instead of
if (game.chkWin == 1)
Poor:
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
Better:
game.setMove(choice_num, 1);
switch (game.chkWin()) {
case 1:
cout << name1 << " has won this Game!";
break;
case -1:
cout << name1 << " has won this Game!";
break;
case 0:
cout <<"The Game is a Draw! ";
break;
}
The basic problem is using "==" with function game.chkWin, instead of with the result of function game.chkWin().
But in cases like this (pun intended) a "switch()" block is probably more readable and better style than multiple "if/else" statements.
'Hope that helps...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was just programming in c++, when all of a sudden all the "cout"s and "cin"s were errors and "Ambiguous". Including System.
I don't know why this happened. Everything was fine, I was coding the same program for about 2 hours, when it just... happened.
EDIT
I can still run the program without errors, but they show as errors on the text, the red scribbly line. What happened?
I'm using the Visual Studio 2013 IDE, whatever it comes with.
#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
struct Gun
{
string name;
int damage;
int cost;
bool purchased;
bool equipped;
} M4A1, FAMAS;
//WEAPONS INFO
M4A1.cost = 50;
M4A1.damage = 5;
M4A1.purchased = false;
M4A1.equipped = false;
FAMAS.cost = 300;
FAMAS.damage = 10;
FAMAS.purchased = false;
FAMAS.equipped = false;
//WEAPONS INFO
//-----PLAYER(BEGIN)-----
struct Player
{
int health;
string name;
int money;
int energy;
string l_a;
string r_a;
string l_l;
string r_l;
string rank;
} Player;
//GAME PLAYER BEGIN
Player.l_a = "Normal";
Player.r_a = "Normal";
Player.l_l = "Normal";
Player.r_l = "Normal";
Player.health = 100;
Player.money = 100;
Player.energy = 100;
string plyrname;
string rank = "Private";
Player.name = plyrname;
//GAME PLAYER END
//-----PLAYER(END)-----
cout << "What is your name? ";
cin >> plyrname;
goto mmenu;
mmenu:
//-----MAIN MENU(BEGIN)-----
system("CLS");
if (Player.l_a == "Damaged")
{
cout << "Your Left Arm is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.r_a == "Damaged")
{
cout << "Your Right Arm is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.l_l == "Damaged")
{
cout << "Your Left Leg is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.r_l == "Damaged")
{
cout << "Your Right Leg is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.money >= 500 && Player.rank == "Private")
{
system("CLS");
cout << "You have been promoted to Private 2!";
Player.rank = "Private 2";
Sleep(1600);
}
if (Player.money >= 1000 && Player.rank == "Private 2")
{
system("CLS");
cout << "You have been promoted to Private First Class!";
Player.rank = "Private First Class";
Sleep(1600);
}
system("CLS");
cout << "Health: " << Player.health << ". Money: " << Player.money << " dollars." << " Energy: " << Player.energy;
if (M4A1.equipped)
cout << "\nGun: M4A1";
if (FAMAS.equipped)
cout << "\nGun: FAMAS";
cout << "\n\nRank: " << Player.rank;
cout << "\n\n1) Go to Gunstore\n2) Sleep\n3) Fight\n\nAction: ";
int mmenuch1;
cin >> mmenuch1;
if (mmenuch1 == 1)
{
goto gunstore;
}
if (mmenuch1 == 2)
{
system("CLS");
cout << "You sleep, restoring your energy.";
Player.energy = 100;
if (Player.l_a == "Damaged")
{
cout << "\n\nYour Left Arm was healed.";
Player.l_a = "Normal";
}
if (Player.r_a == "Damaged")
{
cout << "\n\nYour Right Arm was healed.";
Player.r_a = "Normal";
}
if (Player.l_l == "Damaged")
{
cout << "\n\nYour Left Leg was healed.";
Player.l_l = "Normal";
}
if (Player.r_l == "Damaged")
{
cout << "Your Right Leg was healed.";
}
Sleep(1400);
goto mmenu;
}
if (mmenuch1 == 3)
{
system("CLS");
goto fight;
}
//-----MAIN MENU(END)-----
fight:
srand(time(0));
system("CLS");
if (Player.r_a == "Damaged" || Player.r_l == "Damaged" || Player.l_a == "Damaged" || Player.l_l == "Damaged")
{
cout << "You're injured, sleep to heal.";
Sleep(1400);
goto mmenu;
}
if (Player.energy < 40)
{
cout << "You don't have enough energy to fight.";
Sleep(1400);
goto mmenu;
}
if (M4A1.equipped == false && FAMAS.equipped == false)
{
cout << "You don't have a gun equipped.";
Sleep(1400);
goto gunstore;
}
if (M4A1.equipped == true && Player.energy > 40)
{
int randnum1 = rand() % (M4A1.damage - 2 + 1) + 2;
Player.money = Player.money + (randnum1 * 15);
Player.energy = Player.energy - 40;
int randnum3 = rand() % (10 - 1 + 1) + 1;
if (randnum3 < 4)
{
int randnum4 = rand() % (13 - 1 + 1) + 1;
if (randnum4 < 3)
{
Player.l_a = "Damaged";
}
if (randnum4 <= 6 && randnum4 >= 4)
{
Player.r_a = "Damaged";
}
if (randnum4 <= 9 && randnum4 >= 7)
{
Player.l_l = "Damaged";
}
if (randnum4 <= 13 && randnum4 >= 10)
{
Player.r_l = "Damaged";
}
}
cout << "You fight, killing " << randnum1 << " enemies, making " << randnum1 * 15 << " dollars!";
Sleep(1600);
goto mmenu;
}
if (FAMAS.equipped == true && Player.energy > 40)
{
int randnum2 = rand() % (FAMAS.damage - 4 + 1) + 4;
Player.money = Player.money + (randnum2 * 15);
Player.energy = Player.energy - 40;
int randnum5 = rand() % (10 - 1 + 1) + 1;
if (randnum5 < 4)
{
int randnum6 = rand() % (13 - 1 + 1) + 1;
if (randnum6 < 3)
{
Player.l_a = "Damaged";
}
if (randnum6 <= 6 && randnum6 >= 4)
{
Player.r_a = "Damaged";
}
if (randnum6 <= 9 && randnum6 >= 7)
{
Player.l_l = "Damaged";
}
if (randnum6 <= 13 && randnum6 >= 10)
{
Player.r_l = "Damaged";
}
}
cout << "You fight, killing " << randnum2 << " enemies, making " << randnum2 * 15 << " dollars!";
Sleep(1600);
goto mmenu;
}
//-----GUNSTORE(BEGIN)-----
gunstore:
system("CLS");
cout << "Welcome to the gunstore! You have " << Player.money << " dollars.";
cout << "\n\n1)M4A1 | Assault Rifle | $50\n2)FAMAS | Assault Rifle | $300\n\n3)Back\n\nAction: ";
int gschoice1;
cin >> gschoice1;
if (gschoice1 == 1)
{
goto prchs_M4A1;
}
else if (gschoice1 == 2)
{
goto prchs_FAMAS;
}
else if (gschoice1 == 3)
{
goto mmenu;
}
prchs_M4A1:
system("CLS");
if (M4A1.purchased == true)
{
cout << "You already purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice6;
cin >> gschoice6;
if (gschoice6 == 1)
{
system("CLS");
M4A1.equipped = true;
FAMAS.equipped = false;
goto mmenu;
}
else if (gschoice6 == 2)
{
goto gunstore;
}
}
if (Player.money >= 0)
{
system("CLS");
cout << "Would you like to buy the M4A1?";
cout << "\n\n1)Yes\n2)No\n\nAction: ";
int gschoice2;
cin >> gschoice2;
if (gschoice2 == 1)
{
system("CLS");
Player.money = Player.money - M4A1.cost;
M4A1.purchased = true;
cout << "You've purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice3;
cin >> gschoice3;
if (gschoice3 == 1)
{
system("CLS");
M4A1.equipped = true;
FAMAS.equipped = false;
cout << "You've equipped the M4A1";
Sleep(1400);
goto gunstore;
}
if (gschoice3 == 2)
{
system("CLS");
M4A1.equipped = false;
goto gunstore;
}
}
if (gschoice2 == 2)
{
system("CLS");
goto gunstore;
}
}
else if (Player.money < 0)
{
system("CLS");
cout << "You don't have enough money.";
Sleep(1400);
goto gunstore;
}
prchs_FAMAS:
if (FAMAS.purchased == true)
{
cout << "You already purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice7;
cin >> gschoice7;
if (gschoice7 == 1)
{
system("CLS");
FAMAS.equipped = true;
M4A1.equipped = false;
goto mmenu;
}
else if (gschoice7 == 2)
{
goto gunstore;
}
}
if (Player.money >= 100)
{
system("CLS");
cout << "Would you like to buy the FAMAS?";
cout << "\n\n1)Yes\n2)No\n\nAction: ";
int gschoice4;
cin >> gschoice4;
if (gschoice4 == 1)
{
system("CLS");
Player.money = Player.money - FAMAS.cost;
FAMAS.purchased = true;
cout << "You've purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice5;
cin >> gschoice5;
if (gschoice5 == 1)
{
system("CLS");
FAMAS.equipped = true;
M4A1.equipped = false;
cout << "You've equipped the FAMAS";
Sleep(1400);
goto gunstore;
}
if (gschoice5 == 2)
{
system("CLS");
FAMAS.equipped = false;
goto gunstore;
}
}
if (gschoice4 == 2)
{
system("CLS");
goto gunstore;
}
}
else if (Player.money < 100)
{
system("CLS");
cout << "You don't have enough money.";
Sleep(1400);
goto gunstore;
}
//-----GUNSTORE-----
}
This kind of thing doesn't just magically happen on its own; you changed something! In industry we use version control to make regular savepoints, so when something goes wrong we can trace back the specific changes we made that resulted in that problem.
Since you haven't done that here, we can only really guess. In Visual Studio, Intellisense (the technology that gives you auto-complete dropdowns and those squiggly red lines) works separately from the actual C++ compiler under the bonnet, and sometimes gets things a bit wrong.
In this case I'd ask why you're including both cstdlib and stdlib.h; you should only use one of them, and I recommend the former. They are basically the same header, a C header, but cstdlib puts them in the namespace std in order to "C++-ise" them. In theory, including both wouldn't conflict but, well, this is Microsoft we're talking about. Their C++ toolchain sometimes leaves something to be desired. Any time the Intellisense disagrees with the compiler has to be considered a bug, whichever way you look at it!
Anyway, your use of using namespace std (which I would recommend against, in future) means that std::system from cstdlib now conflicts with system from stdlib.h. I can't explain what's going on with std::cout and std::cin.
Try removing #include <stdlib.h> and see what happens.
If your program is building successfully then you don't need to worry too much about this, but I can imagine the false positives being annoying when you're working in your IDE.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some functions that need to be called multiple times, as such
int i;
i = 10;
while (i > 0)
{
selectletter(wordArray);
computerTurn(wordArray);
printGrid(grid);
i--;
}
The function selectletter works fine, and near the end of that function, it calls another function, "claimword". Claimword runs entirely fine, but at the end of the function, the program crashes when it runs out of context, as opposed to it just moving on to computerTurn as it should as shown above. I looked up on SO how to "exit" a function, and everyone said that "return;" would work fine, even in a void function. However, when I try using return, nothing at all happens, except for anything after the return statement is ignored. Can anyone tell me why the return statement doesn't work?
void claimword(Tile grid[7][6], char letter, string wordArray[100])
{
cout << "Would you like to claim a word? (Y/N)" << endl;
char chooseinput;
cin >> chooseinput;
if ((chooseinput == 'y') || (chooseinput == 'Y'))
{
printGrid(grid);
cout << "Please enter the word you would like to claim." << endl;
string input;
cin >> input;
int inthegrid = 0;
int errormessage = 0;
compchecker(grid, input, inthegrid);
int length;
if (inthegrid = 1)
{
for(int i = 0; i < 100; ++i)
{
if (input == wordArray[i])
{
if (input.find(letter) != std::string::npos)
{
string strl;
strl = wordArray[i];
length = strl.length();
cout << "You have claimed the word " << strl << endl;
wordArray[i] = "/";
}
else
{
errormessage = 1;
}
}
else
{
///cout << "Sorry, that word is not in the dictionary." << endl;
errormessage = 2;
}
}
if (errormessage = 1)
{
cout << "Sorry you cannot claim that word." << endl;
}
if (errormessage = 2)
{
cout << "Sorry, that word is not in the dictionary." << endl;
}
if (length == 3)
{
human.humanpoints = human.humanpoints + 1;
wordsthisturn = wordsthisturn + 1;
cout << "You have earned one point!" << endl;
}
if (length == 4)
{
human.humanpoints = human.humanpoints + 2;
wordsthisturn = wordsthisturn + 2;
cout << "You have earned two points!" << endl;
}
if (length == 5)
{
human.humanpoints = human.humanpoints + 4;
wordsthisturn = wordsthisturn + 4;
cout << "You have earned four points!" << endl;
}
if (length == 6)
{
human.humanpoints = human.humanpoints + 8;
wordsthisturn = wordsthisturn + 8;
cout << "You have earned eight points!" << endl;
}
if (length == 7)
{
human.humanpoints = human.humanpoints + 16;
wordsthisturn = wordsthisturn + 16;
cout << "You have earned sixteen points!" << endl;
}
else
{
cout << "Your word was too small to claim any points." << endl;
}
}
}
else
{
cout << "End of Player Turn." << endl;
//return;
}
cout <<"Test1";
return;
cout <<"Test2";
}
Regardless of the input I give it (y/n and such), "Test1" displays, but "Test2" doesn't. My theory is that the program doesn't return all the way, or I'm just simply not using it right.
EDIT:
With an edited statement in the main function,
selectletter(wordArray);
cout << "test11";
computerTurn(wordArray);
What should happen is that the selectletter function should be called. The selectletter function, at the end of it, calls another function, claimWord. claimWord is posted above. At the end of the function, it should end. There should be nothing left for it to do, and after all those if/elses regarding points, and even if no points are scored, or anything in the function happens, the function should end. The program should then display "test11", but it does not.
EDIT2:
void selectletter(string wordArray[100])
{
cout << endl;
cout << "REMAINING LETTERS:" << endl;
cout << human.humanletters << endl;
cout << "Select a letter.";
int length;
length = human.humanletters.size();
char input;
cin >> input;
int column;
int row = 7;
int cinput;
//mght have to change since 0 is the first val
cout << "What column would you like to drop that in? (1-7)";
cin >> cinput;
column = cinput - 1;
//cout << "Test1";
while (row > 0)
{
if (grid[row-1][column].active == true)
{
row--;
//cout << "Test3";
}
else
for(int i = 0; i < length; i++)
{
if(human.humanletters[i] == input)
{
//cout << "Test5";
human.humanletters.erase(std::remove(human.humanletters.begin(), human.humanletters.end(), input), human.humanletters.end());
grid[row-1][column].letter = input;
grid[row-1][column].active = true;
cout << endl;
//cout << "Test6";
claimword(grid, input, wordArray);
//this removes ALL instances of the letter, however
}
break;
//need to add something for if the letter is not in the string
//}
//row = 9999;
}
}
}
Regardless of the input I give it (y/n and such), "Test1" displays, but "Test2" doesn't.
That is what it is supposed to do. You called return after displaying Test1 and before displaying Test2, so the latter was skipped. return is an immediate return to the function that called the current function.
Your while loop has the condition while (row > 0), and you only decrement row when (grid[row-1][column].active == true). If that ever evaluates to false, you won't decrement row and your program runs forever.
Perhaps your break; was meant to break out of the while loop, but all it will do is break out of the for loop. A break statement breaks out of the nearest enclosing loop/switch block.
I have a code that is supposed to use user input to take an order of a pizza.
#include<iostream>
#include<string>
using namespace std;
int main()
{
double totalCosts = 0;
// PIZZA
cout << "What size is your first pizza?\n"
"small: $8.00\n"
"medium: $10.00\n"
"large: $12.00\n";
string currentSize = "";
getline(cin, currentSize);
cout << "\n";
if(currentSize == "small")
{
totalCosts += 8;
}
else if(currentSize == "medium")
{
totalCosts += 10;
}
else if(currentSize == "large")
{
totalCosts += 12;
}
else
{
cout << "invalid size: " << currentSize;
return 0;
}
string pizzaToppings = "";
cout << "Choose from the following toppings:\n"
"onions: $1.00\n"
"peppers: $1.20\n"
"ham: $1.50\n"
"hamburger: $1.25\n"
"pepperoni: $1.55\n"
"salami: $1.63\n"
"sausage: $1.44\n";
getline(cin, pizzaToppings);
cout << "\n";
if(pizzaToppings.find("onions") != string::npos)
{
totalCosts += 1;
}
else if(pizzaToppings.find("peppers") != string::npos)
{
totalCosts += 1.2;
}
else if(pizzaToppings.find("ham") != string::npos)
{
totalCosts += 1.5;
}
else if(pizzaToppings.find("hamburger") != string::npos)
{
totalCosts += 1.25;
}
else if(pizzaToppings.find("pepperoni") != string::npos)
{
totalCosts += 1.55;
}
else if(pizzaToppings.find("salami") != string::npos)
{
totalCosts += 1.63;
}
else if(pizzaToppings.find("sausage") != string::npos)
{
totalCosts += 1.44;
}
else
{
cout << "Choose at least one valid topping";
return 0;
}
// DELIVERY AND FINAL PRINTOUT
string needToDeliver = "";
cout << "Do you want delivery? yes/no\n";
getline(cin, needToDeliver);
cout << "\n";
if(needToDeliver == "yes")
{
totalCosts += 3;
cout << "What is your address?";
getline(cin, needToDeliver); // address doesn't matter
}
cout << "Your total cost is: $" << totalCosts << ". Thank you for buying C++ Pizza.";
return 0;
}
This code gives the following output:
What size is your first pizza?
small: $8.00
medium: $10.00
large: $12.00
invalid size:
What I think is happening is the first getline function is not waiting for any user input, and is instead inputing an empty string. Why is it doing this?