When I run the program, I can't figure out why when I ask for the letter a, b, or c and a different letter is inputted other than those, why is it going to "invalid criteria" instead of "An invalid code has been entered. When I input a number out of the range it goes to "Invalid number has been entered" but not when I'm asking for the letter
#include <iostream>
#include <iomanip>
#include "Header.h"
using namespace std;
int main()
{
head();
int num_1, a, b, c;
char char_1;
float num_2;
num_2 = 28.82;
a = b = c = 0;
cout << "Please enter a number between 10 and 30." << endl;
cin >> num_1;
if (num_1 >= 10 && num_1 <= 30)
{
cout << "Enter the letter a, b, or c." << endl;
cin >> char_1;
if (char_1 == 'a'||'b'||'c')
{
if ((num_1 >= 10 && num_1 <= 20) && (char_1 == 'a'))
{
num_2 = num_2 + .5;
cout << fixed << setprecision(1) << num_2 << endl;
}
else if ((num_1 >= 19 && num_1 <= 30) && (char_1 == 'b'))
{
num_2 = num_2 + .10;
cout << fixed << setprecision(2) << num_2 << endl;
}
else if ((num_1 >= 19 && num_1 <= 30) && (char_1 == 'c'))
{
num_2 = num_2 + .100;
cout << fixed << setprecision(3) << num_2 << endl;
}
else
{
cout << "Invalid criteria" << endl;
}
}
else
cout << "An invalid code has been entered."
}
else
cout << "An invalid number has been entered." << endl;
system("pause");
return 0;
}
The expression:
char_1 == 'a' || 'b' || 'c'
is equivalent to:
char_1 == ('a' || 'b' || 'c')
and therefore first evaluates the logical or of all those letters treated as booleans (all true hence the result is true), then compares that with your variable.
What you need is:
(char_1 == 'a') || (char_1 =='b') || (char_1 =='c')
This checks the character against each of the possibilities and then works out whether any of them were true.
Related
I'm doing a school project that creates a blackjack game, but unfortunately, I'm stumped at a few errors I must have made earlier on. The errors appear on lines 165 and 173. It states that I'm missing several elements that I cannot piece together. Could someone help inform me of what's wrong with this program that won't compile?
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
using namespace std;
int getCard()
{
return rand() % 10 + 1;
}
char readChar()
{
char c;
cin >> c;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return c;
}
int main()
{
//set srand to seed system clock
//to generate random number
srand((int)time(0));
char yesno = 'y';
do
{
int dealer = getCard();
int first = getCard(), second = getCard();
int total = first + second;
if (yesno == 'y' || yesno == 'Y')
{
cout << "The dealer starts with a " << dealer << endl;
cout << "Your first cards: " << first << ", " << second << endl;
cout << "Total: " << total << endl;
do
{
cout << "hit? (y/n): ";
yesno = readChar();
if (yesno == 'y' || yesno == 'Y')
{
int newCard = getCard();
total += newCard;
cout << "Card: " << newCard << endl;
cout << "Total: " << total << endl;
if (total > 21)
{
cout << "Bust" << endl;
yesno = 'n';
break;
}
else if(total == 21)
{
cout << "Blackjack" << endl;
}
else if (yesno == 'n' || yesno == 'N')
{
//Dealer must take more cards if less than 17
while (dealer < 17)
{
cout << "Dealer has a " << dealer << "..." << endl;
char c;
do {
cout << "(c to continue) ";
c = readChar();
} while (c != 'c' && c != 'C');
int newDealerCard = getCard();
cout << "Dealer gets a " << newDealerCard << endl;
dealer += newDealerCard;
cout << "Total: " << dealer << endl;
}
//determine winner
if (dealer == 21 && dealer != total)
cout << "Lose" << endl;
else if (dealer == total)
cout << "Push" << endl;
else if (dealer > 21 && total > dealer && total < 21)
cout << "Win" << endl;
else if (dealer > total && dealer <= 21)
cout << "Lose" << endl;
break;
}
} while (yesno != 'n' && yesno != 'N');
}
cout << "Would you like to play again? (y/n) : ";
yesno = readChar();
} while (yesno != 'n' && yesno != 'N');
return 0;
}
Since I was a bit lost at the end, I wasn't sure where to add my missing elements.
There are at least two do {...} while (...) loops in there that do not have while clauses.
I want to reverse the input string; this is the code I wrote, but I am unable to print the reverse of the string.
#include<iostream>
using namespace std;
void main() {
char Title[] = "\t THE ANALYZER";
char phrase[501];
int charTotal = 0;
int numTotal = 0;
int letterTotal = 0;
int vowelTotal = 0;
int consonTotal = 0;
int spacesTotal = 0;
int specialCharTotal = 0;
cout << Title<< endl;
cout << "\t ____________" << endl;
cout << " Enter your phrase (Max 500): ";
cin.getline(phrase, 501);
cout << " Thanks, Analyzing..." << endl;
cout << " These are the informations about your phrase" << endl;
for (int i = 0; phrase[i] != '\0' ; i += 1) {
if (phrase[i] != '\0') {
charTotal++;
}
if (phrase[i] >= '0' && phrase[i] <= '9') {
numTotal++;
}
if ((phrase[i] >= 'A' && phrase[i] <= 'Z') || (phrase[i] >= 'a' && phrase[i] <= 'z')) {
letterTotal++;
}
if (phrase[i] == 'a' || phrase[i] == 'A' || phrase[i] == 'e' || phrase[i] == 'E' || phrase[i] == 'i' || phrase[i] == 'I' || phrase[i] == 'o' || phrase[i] == 'O' || phrase[i] == 'u' || phrase[i] == 'U') {
vowelTotal++;
}
consonTotal = letterTotal - vowelTotal;
if (phrase[i] == ' ') {
spacesTotal++;
}
specialCharTotal = (charTotal - (numTotal + letterTotal + spacesTotal));
}
cout << " Number of characters: " << charTotal << endl;
cout << " Number of numerics: " << numTotal << endl;
cout << " Number of alphabets: " << letterTotal << endl;
cout << "\tNumber of vowels: " << vowelTotal << endl;
cout << "\tNumber of consonants: " << consonTotal << endl;
cout << " Number of spaces: " << spacesTotal << endl;
cout << " Number of special characters: " << specialCharTotal << endl;
cout << " Your phrase in reverse: " << endl'
system("pause");
}
Make use of std::reverse:
std::string s = "reverse me";
std::reverse(s.begin(), s.end());
The goal is to calculate the percentage of marks over 10. Marks are between 0 and 20. I have an issue with the while loop when I click on 'N'. I get an infinite loop.
#include <iostream>
int main() {
float MARK, PERCENTAGE;
int NBR_MARK, NBR_MARK_10;
MARK = 0;
NBR_MARK = 0;
NBR_MARK_10 = 0;
PERCENTAGE = 0;
char R = 'N';
std::cout << "Enter a mark ?" << std::endl;
std::cin >> MARK;
while (MARK < 0 || MARK > 20) {
std::cout << " Please, enter a mark between 0 and 20" << std::endl;
std::cin >> MARK;
}
std::cout << "Do you want to enter a new mark ?" << std::endl;
std::cout << "Click on 'O' to continue and on 'N' to stop" << std::endl;
std::cin >> R;
if ((R != 'N') || (R != 'n'))
std::cout << "You will continue" << std::endl;
do {
std::cout << "Enter a new mark" << std::endl;
std::cin >> MARK;
std::cout << std::endl;
while ((MARK < 0) || (MARK > 20)) {
std::cout << "Please, enter a mark between 0 and 20" << std::endl;
std::cin >> MARK;
std::cout << std::endl;
}
NBR_MARK++;
if (MARK > 10) NBR_MARK_10++;
std::cout << "To stop press 'N'" << std::endl;
std::cin >> R;
}
while ((R != 'N') || (R <= 'n'));
PERCENTAGE = NBR_MARK_10 / NBR_MARK * 100;
std::cout << "Le % de notes > 10 est de: " << PERCENTAGE << " %" << std::endl;
return 0;
}
#include<iostream>
using namespace std;
int main() {
float MARK=0.0, PERCENTAGE=0.0;
int NBR_MARK_10;
float NBR_MARK = 0.0;
NBR_MARK_10 = 0;
char R = 'a';
while(R!='N')
{
std::cout << "Enter a mark ?" << std::endl;
std::cin >> MARK;
while (MARK < 0 || MARK > 20) {
std::cout << " Please, enter a mark between 0 and 20" << std::endl;
std::cin >> MARK;
}
NBR_MARK+=1.0;
if (MARK > 10) NBR_MARK_10++;
std::cout << "Do you want to enter a new mark ?" << std::endl;
std::cout << "Click on 'O' to continue and on 'N' to stop" << std::endl;
cin >> R;
}
PERCENTAGE = (NBR_MARK_10 / NBR_MARK) * 100;
std::cout << "Le % de notes > 10 est de: " << PERCENTAGE << " %" << std::endl;
return 0;
}
use a while loop when your checking the next input of R and make sure you make one of the counter variables to float else youll end up getting percentage as zero for few testcases.
You have two problems with the code. As Sharath Chander P pointed out, there is clearly a logical issue in the code flow. Even if you press 'N' (for the first time) the code block inside do..while will execute since the condition is checked only after executing all the statements inside the do..while loop.
Now to your question on the infinite loop. It is because the expression while ((R != 'N') || (R <= 'n')); will be 'true' when you press the key 'N'. When you press 'N', the value of R will be 78 (ASCII value of 'N'). Since the ASCII value of 'n' is 110, the checking R<='n' will return true. (Clearly 78 is less than 110.)
Since (R!='N') is false and R<='n' is true, the expression while ((R != 'N') || (R <= 'n')); will be evaluated as while(false || true).
Since the result will be "true", the loop will continue.
Now if you press 'n' then the expression will be evaluated as while(true || true) that too will result in "true" and the loop will continue;
#include <iostream>
using namespace std;
main()
{
char a[2];
cout << " \"Welcome to Virtual University\" " << endl;
cout << " Program to predict user programming level" << endl;
cout << " Please answer in either T=True or F=False" << endl;
cout << "Q:1 Switch is a loop?" << endl;
cout << "A: ";
cin >> a[0];
cout << "Q:2 Pointer stores memory address?" << endl;
cout << "A: ";
cin >> a[1];
cout << "Q:3 Semicolon after for loop is an error? " << endl;
cout << "A: ";
cin >> a[2];
if((a[0] == 'f') || (a[0] == 'F') && (a[1] == 't') || (a[1] == 'T') && (a[2]
== 'f') || (a[2] == 'F'))
{
cout << "Your programming level is advanced" << endl;
}
else if((a[0] == 'f') || (a[0] == 'F') && (a[1] == 't' ) || (a[1] == 'T')
&& (a[2]== 't') || (a[2] == 'T'))
{
cout << "Your programming level is intermediate" << endl;
}
else if((a[0] != 'f') || (a[0] != 't') && (a[1] != 't' ) || (a[1] != 'f') &&
(a[2] != 't') || (a[2] != 'f'))
{
cout << "please enter t or f only" << endl;
}
else
{
cout << "Your programing level is begginner" << endl;
}
return 0;
}
its not giving any errors in compiler but when i run this`` program it always prints the wrong statement. can you guyz please help me.
and when i remove the || conditions with capital F and T. its working fine.
can any one help putting those capital letters as well because its a requirement in my assignment. thanks in advance.
This question already has answers here:
Why is this only returning "yes"
(2 answers)
Closed 6 years ago.
I'm making my first project c++. It's a simple temperature converter.
I made a test section [code 1] with if statements. the if statement would compare the user input. for example if you user typed c and then k(Celsius-Kelvin). it should run the function[code 2] CtoK(); but i doesn't it runs all function why does it do this?
It try to use return but i didn't(it also didn't gave a error so i kept it)
If you guys see something else pls say it Code on pastebin
Also thinks to keep it mind:
Just stated to learn C++
Not native English so if there are spelling and grammar mistakes please say it so i can learn form it
[code 1]
void whatToWhat(char firstDegrees, char secondDegrees) {
if (firstDegrees == 'C' || 'c') {// tests if the user want form c to f
if (secondDegrees == 'F' || 'f') {
CtoF();
}
}if (firstDegrees == 'C' || 'c') {// tests if the user want form c to k
if (secondDegrees == 'K' || 'k') {
CtoK();
}
}if (firstDegrees == 'F' || 'f') {// tests if the user want form f to c
if (secondDegrees == 'C' || 'c') {
FtoC();
}
}if (firstDegrees == 'F' || 'f') {// tests if the user want form f to k
if (secondDegrees == 'K' || 'k') {
FtoK();
}
}if (firstDegrees == 'K' || 'k') {// tests if the user want form k to f
if (secondDegrees == 'F' || 'f') {
KtoF();
}
}if (firstDegrees == 'K' || 'k') {// tests if the user want form k to c
if (secondDegrees == 'C' || 'c') {
KtoC();
}
}
}
[code 2]
void CtoF() {// c to f furmula
double input;
cout << "Enter a number[Celsius-Fahrenheit]" << endl;
cin >> input;
cout << "it's " << input * 1.8 + 32 << " Fahrenheit " << endl;
return;
}
void CtoK() {// c to k furmula
double input;
cout << "Enter a number[Celsius-Kelvin]" << endl;
cin >> input;
cout << "it's " << input + 273.15 << " Kelvin " << endl;
return;
}
void FtoC() {//f to c furmula
double input;
cout << "Enter a number[Fahrenheit-Celsius]" << endl;
cin >> input;
cout << "it's " << input / 1.8 - 32 << " Celsius " << endl;
}
void FtoK() {//f to k furmula
double input;
cout << "Enter a number[Fahrenheit-Kelvin]" << endl;
cin >> input;
cout << "it's " << input / 1.8 - 32 + 273.15 << " Kelvin " << endl;
return;
}
void KtoF() {// k to f furmula
double input;
cout << "Enter a number[Kelvin-Fahrenheit]" << endl;
cin >> input;
cout << "it's " << (input - 273.15) * 1.8 + 32 << " Fahrenheit " << endl;
}
void KtoC() {// k to c furmula
double input;
cout << "Enter a number[Kelvin-Celsius]" << endl;
cin >> input;
cout << "it's " <<273.15 - input << " Celsius " << endl;
return;
}
if(firstDegrees == 'K' || 'k') will always evaluate to true since k as it is is Not Null, means Valid, means True.
You need to write all your expressions in a similar way to this: (firstDegrees == 'K' || firstDegrees == 'k')
Also, you would want to add elses after each if, for better and clearer logic control.