Conditional statement outputting the wrong command [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
Beginner here so I'm sorry if I made nooby mistakes
I assign di to be the array myworld[] depending the the user input it'll assign the di into the appropriate array position, but for some reason the if statement keep outputting "make" instead of "change" when my input is 'c'
I tried to remove else if and put if for all of them, or got rid of else if and just use else.
#include <iostream>
using namespace std;
int main() {
char di;
char myword[] = {'d','m','s' ,'c'};
do {
cout << "Make a selection:" << endl;
cout << "d - insert 1$ bill" << endl;
cout << "m - view menu" << endl;
cout << "s - select an item" << endl;
cout << "c - get change" << endl;
cin >> di;
if (di == 'd')
di = myword[0];
else if (di == 'c')
di = myword[3];
}while (!myword);
if (myword[0])
cout << "make";
else if (myword[3])
cout << "change";
return 0;
}

Probably you forgot to make a comparison inside if statement. For now you are just saying if('d'!= 0) which is always true. Perhaps you tried to make if(di == myword[0]). The same applies for the else if statement.

Related

(Total Beginner) Unsure why goto does not work in program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 months ago.
Improve this question
Hey so I only started C++ 2 days ago, so please try to keep the answer simple, otherwise probably won't understand it, thanks!
I tried to make a basic program where a program asks for a word, then counts the letters in said word. It then tells asks if you want to know the letter in any given position of the letter. Im making this because I thought I would learn better if I just tried making something basic rather than endlessly watching videos on it.
I ran into a problem with the asking the user part of the code. I want to have it check whether the user typed Y or N, and if neither, repeat asking until either Y or N is inputted. I tried using goto, but I could not get it to work despite checking online tutorials on how it should work. If anyone could help that would be greatly appreciated :).
When run, it does the following: enter image description here
The code is below, thank you for reading:
#include <string>
using namespace std;
int main(){
string text; //variable for the word to be measured
int letter; //variable for the placement of the letter in word
string confirmation; //variable for Y or N
cout << "This program counts letters in a word \n\n";
cout << " Please type a word for me to count: \n\n";
cin >> text;
cout << "\nYour word has " << text.length() << " letter";
if (text.length()>1){
cout << "s"; // Checks whether to put an s at the end of the prev. sentence for a plural or not
}
cout << "\n\nThis is what you typed by the way: " << text << "\n\n";
cout << "Would you like me to find the letter in any given position in the word? \n\n If yes, type Y. If no, type N: \n\n";
cin >> confirmation;
check:
if (confirmation == "Y"){ //Loops until one of these are fulfilled
cout << "What position's letter would you like me to find? \n\n";
cin >> letter;
cout << "\n" << text[letter-1] << "\n\n";
cout << "Thanks for using me, have a nice day";
} else if (confirmation == "N"){
cout << "\nAlright have a nice day";
} else {
goto check;
}
return 0;
}
First, goto-Syntax is something you do not need to learn as a beginner. In 99.99% there are better alternatives than using goto, so until you are very advanced, just pretend that goto does not exist in C++.
Second, the goto in your code works. It is just that if the user answers something different than "Y" or "N", your code will infinitely loop between the label check: and the goto check statement, as there is no way that confirmation can change in between.
Last, here is an example how to better do this, using a while-loop.
cout << "Would you like me to find the letter in any given position in the word? \n\n If yes, type Y. If no, type N: \n\n";
cin >> confirmation;
while (confirmation != "Y" && confirmation != "N") { //Loops until one of these are fulfilled
cout << "Please answer Y or N.\n";
cin >> confirmation;
}
if (confirmation == "Y"){
cout << "What position's letter would you like me to find? \n\n";
cin >> letter;
cout << "\n" << text[letter-1] << "\n\n";
cout << "Thanks for using me, have a nice day";
} else {
cout << "\nAlright have a nice day";
}

Loop not accepting lowercase even with toupper (C++) [closed]

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
Currently, I want my code to accept lowercase or uppercase a, b, c, or u as a valid entry from the user. However, anytime I enter the characters as lowercase, they respond with the error message and continue the loop until it is put in uppercase. I am new to C++, so I might be using toupper wrong.
#include <iostream>
using namespace std;
int main()
{
bool custGBTypeValid = false;
bool custPlnTypeValid = false;
char custPlanType = toupper('Z');
int custUsedData = 1;
cout << "Hello, welcome to AT&T wireless. We're here to help you decide if your current plan is what's right for you." << endl;
cout << "Here are our plans:" << endl;
cout << "Plan A: For $25 per month 0GB are provided. Data is $10 per GB." << endl;
cout << "Plan B: For $45 per month 2GB are provided." << endl;
cout << "Plan C: For $80 per month 6GB are provided." << endl;
cout << "Plan Unlimited: Unlimited data for $100 per month." << endl;
while (custPlnTypeValid == false)
{
cout << "What type of plan are you on? (Please answer with A, B, C, or U): ";
cin >> custPlanType;
if (custPlanType == toupper('A') || custPlanType == toupper('B') || custPlanType == toupper('C') || custPlanType == toupper('U'))
custPlnTypeValid = true;
else
cout << "ERROR: Incorrect data type entered." << endl;
}
}
How would I get it to accept lowercase too? I have also tried changing each in the if statement to custPlanType == toupper('a') etc. and toupper(custPlanType == 'A') but this doesn't work either. The latter works if the characters within the code are lowercase, but then refuses to work with uppercase characters.
It should be:
if (toupper(custPlanType) == 'A' ....)

C++ How check characters in real time with _getch() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
i need in my loop get character in realtime and check it by conditions. If user press whatever except enter, program works fine. Can anyone help me ? thanks !
while (read != '\n')
{
cout << "Enter character:\n";
read = _getwch();
if (read == '\n') {
cout << "You pressed : ENTER\n";
}
else {
cout << "Your character is: \"" << read << "\"\n\n";
read = '\0';
}
}
include
using namespace std;
int main()
{
cout << "Press the ENTER key";
if (cin.get() == '\n')
{
cout << "Good job.\n";
}
else
{
cout << "I meant ONLY the ENTER key... Oh well.\n";
}
return 0;
}
This code will help in detecting the ENTER key when pressed.
Hope this helps you.

Cannot exit while loop with boolean condition c++ [closed]

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 years ago.
Improve this question
Hi this is my first post. I apologize if I'm not following certain rules or conventions. If that is the case please let me know.
I have a game which runs in a while loop until the score limit is reached by either player, at which point the other player has one last (iteration) chance to beat the first players score. However after the score limit is reached, the loop continues to run and the winner is never checked.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int roll();
int playTurn(int);
int main(){
const int LIMIT = 5;
int whoseTurn = 1;
int pnts1 = 0;
int pnts2 = 0;
bool suddenDeath = false; //True when score limit is reached
while(!suddenDeath){
if(pnts1 >= LIMIT || pnts2 >= LIMIT){ //Limit was reached by previous player.
suddenDeath == true; //Next player has 1 turn to win
}
if(whoseTurn == 1){
pnts1 += playTurn(whoseTurn); //Play turn and tally points
whoseTurn = 2; //Swith player for next iteration
}
else if(whoseTurn == 2){
pnts2 += playTurn(whoseTurn);
whoseTurn = 1;
}
cout << "-------------------------------------" << endl //Display score
<< "Player 1 has " << pnts1 << " points" << endl
<< "Player 2 has " << pnts2 << " points" << endl
<< "-------------------------------------" << endl << endl;
};
if(pnts1 > pnts2)
cout << "Congratulations Player 1! You won with a score of: " << pnts1 << " - " << pnts2;
else if(pnts2 > pnts1)
cout << "Congratulations Player 2! You won with a score of: " << pnts2 << " - " << pnts1;
else if(pnts1 == pnts2)
cout << "A tie! What are the chances?";
return 0;
}
suddenDeath == true;
// ^^
is an expression meaning "compare those two values", which is then thrown away. The C statement 42; is equally valid, and equally useless (a).
You want to assign the value, so you'd use:
suddenDeath = true;
// ^
It's actually the other end of the much more common if (a = 0) problem where people assign rather than compare.
(a) If you're wondering why anyone in their right mind would allow this into a language, it actually allows for some powerful constructs with minimal code.
And, you've seen it before most likely. The statement i++; is such a beast. It's an expression giving i (which you throw away here) with the side effect that i is incremented afterwards.
suddenDeath = true;
Use a single = for assignment. == is used for condition check.

What is wrong with this C++ code [closed]

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 years ago.
Improve this question
I'm new to programming and I've decided to try and make a Calculator that can do stuff other than simple Arithmetic. I have not finished yet, I was just testing to see if it was working so far. As I ran it, and went through Arithmetic by pressing 1 it just stops. Can someone please tell me what Ive done wrong? Thank you.
#include <iostream>
using namespace std;
int main()
{
int frsnum
int secnum
int arithchoice;
int answer;
int x;
cout << "Welcome to the advanced calculator!" << endl;
cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
cout << " Systems of Equations < 2 >" << endl;
cout << " Matrices < 3 >" << endl;
cin >> x;
if(x == 1)
{
cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
cin << arithchoice;
}
if(arithchoice == 1)
{
cout << "Whats the first number: "
cin >> frsnum;
cout << "And the second number: "
cin >> secnum;
answer = frsnum + secnum;
cout << "That would be: " answer << endl
}
system("PAUSE");
return 0;
}
The arrows in this statement are incorrect.
cin << arithchoice;
should be replaced by this statement
cin>> arithchoice;
Update
The best way to remember which arrows to use with Cin and Cout is that with when inputing value you are pointing from outside to the computer.
Similarly for cout you throw values from the computer to outside world.
So now if you want to pass values from real world to computer you which arrow will you use >> cin
Similarly for giving results from computer to Real world(user) "<<"
----------------
| |
Real world | <--- computer |
|_______________|
The first thing I've noticed is that in the (x==1) if block, the arrows of the cin are the wrong way round.