I am new to C++ and this is like my first program I made and I used Visual C++ 2010 Express.
It is a weight conversion thing. There is an if loop, an else if loop and an else.
Here is the code:
#include <iostream>
using namespace std;
int main() {
float ay,bee;
char char1;
cout << "Welcome to the Ounce To Gram Converter" << endl << "Would you like to convert [O]unces To Grams or [G]rams To Ounces?" << endl;
start:
cin >> char1;
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
cin.ignore();
cin.get();
return 0;
}
If I enter a "g", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
like it should
However, if I enter an "o", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
Even if I put something random, like "h"
This Happens:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
Please tell me what I did wrong.
char1 = "o"||"O" will always evaluate to true, because "O" is not null.
You want to use char1 == 'o' || char == 'O' and similar all over your if statements.
Note that = is assignment and == is an equality check. Use == when testing for equality and = when assigning. C and C++ allows you to use = in a check which returns the value of the assignment. This value is not 0, which evaluates to true and thus your if statement executes.
Related
I'm creating a rock paper scissors game for an assignment and one of my functions is returning a warning saying "not all control paths return a value". I'm assuming the issue is with the switch statement but I'm not sure as everything in the statement is returning a value.
Here's the code if anyone can help:
int GameChoice()
{
bool loop = true;
while (loop == true)
{
system("cls");
cout << "Choose one of the following:" << endl;
cout << "[1] Rock" << endl;
cout << "[2] Paper" << endl;
cout << "[3] Scissors" << endl;
cout << "[4] Lizard" << endl;
cout << "[5] Spock" << endl;
cout << "[6] End Game" << endl;
cout << "Enter Selection: ";
int UserChoice;
cin >> UserChoice;
cout << endl;
switch (UserChoice)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
loop = false;
return UserChoice;
break;
default:
cout << "Incorrect choice" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
return 0;
}
}
}
I think you can ignore the warning.
In fact, before the first iteration of the while-loop is complete you will reach either return UserChoice; or return 0;.
I guess the compiler expects a return statement after the loop.
I am trying to get the program to read 2 string then convert them to ASCII value and compare the 2 value.
I am not sure how to change the string to ASCII value for the comparing function to work.
#include <iostream>
int main()
{
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
char cha[50];
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if (ch[50] == cha[50]) {
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
return 0;
}
else if (ch > cha) {
std::cout << ch << " is greater than " << cha << std::endl;
std::cout << int(ch) - int(cha) << "is the differents between the two " ;
return 1;
}
else
{
std::cout << ch << " is less than " << cha;
}
system("PAUSE");
}
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
char ch[50] is "array of character" otherwise known as "string"
if (ch[50] == cha[50]) { ... }
Both ch and ch50 have 50 elements. The index starts at zero, that means the last valid element is ch[49]. ch[50] is buffer overflow and causes undefined behavior.
You want instead to declare a single character, char ch; and read that character. Or declare ch[50] and read the string, and do a comparison for ch[0] (that's the first element in the index)
int main()
{
char ch;
char cha;
std::cout << "Enter one character: ";
std::cin >> ch;
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if(ch == cha)
{
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
}
else
{
std::cout << ch << " is greater than " << cha << std::endl;
if(ch > cha)
std::cout << ch << " is greater than " << cha << std::endl;
else
std::cout << ch << " is less than " << cha;
}
return 0;
}
In this program, choices other than 0 to 4 are all invalid so the user is asked for his/her choice again and again. Then, as choice "1" is entered, the program would perform addition of two numbers. Therefore, it would proceed to ask for the two numbers (x and y) and then print out the calculation result.The user can then choose to perform another calculation. As long as the user does not choose the choice "5" to quit the program, the program would continue to do different calculations chosen by the user.I want to exit the program when choice == 5 . Yet it only returns me "Your choice is invalid, please enter again." Here is the code:
#include <iostream>
using namespace std;
int main()
{
int choice, x, y, z;
do
{
cout << "Math is easy!" << endl << "1. Perform addition" << endl << "2. Perform subtraction" << endl <<
"3. Perform multiplication" << endl << "4. Perform division" << endl << "5. Quit" << endl << "Please enter your choice[1 - 5]:" << endl;
cin >> choice;
while (choice < 1 || choice > 4)
{
cerr << "Your choice is invalid, please enter again.";
cin >> choice;
}
cout << "Please enter x:" << endl;
cin >> x;
cout << "Please enter y:" << endl;
cin >> y;
if (choice == 1)
{
z = x + y;
cout << x << "+" << y << "=" << z << endl;
}
if (choice == 2)
{
z = x - y;
cout << x << "-" << y << "=" << z << endl;
}
if (choice == 3)
{
z = x * y;
cout << x << "*" << y << "=" << z << endl;
}
if (choice == 4)
{
z = x / y;
cout << x << "+" << y << "=" << z << endl;
}
} while (choice != 5);
system("pause");
return 0;
}
How should I do?
Before while loop you can check if(choice == 5) and break;
if(choice == 5)// if choice == 5 break loop. or exit(0);
break;
while (choice < 1 || choice > 4)
{
cerr << "Your choice is invalid, please enter again.";
cin >> choice;
}
.
.
.
.
if (choice == 1)
{
z = x + y;
cout << x << "+" << y << "=" << z << endl;
}
else if (choice == 2)
{
//code
}
else if (choice == 3)
{
//code
}
Use if - elseif instead of using multiple if condition. you can also use switch.
O.F community! I'm very new to c++ and I decided to create my own game.
My issue is that at one point in the game the player is asked to answer 3 things:
-Gender
-Age
-Setting
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender; //incomplete - needs //<< setting;
}
The issue is with gender not being displayed. It can cout << ageNumber fine. But when the game is played and this part is reached this is what it says:
Now that the characteristics have been selected. Let's review what they are:
Press any key to continue
1
You are a 16(that is what I put for ageNumber earlier on) year old . <- Notice how "old" and "." have a space? As if the string gender is read as a blank.
EDIT: It is supposed to say:
You are a 16 year old male/female/it.
This is the part that determines the gender.
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
===========================================================================================
This is the full code:
#include <iostream>
#include <string>
using namespace std;
void rules();
void controls();
void start();
void fate();
void assessment();
void setting2();
int fateNumber;
int anykey;
int ageNumber;
int settingNumber;
int genderNumber;
string gender;
string setting;
int main(int argc, char** argv)
{
cout << "===W E L C O M E T O C H O O S E Y O U R F A T E=== \n\n";
cout << "Press any key to continue\n";
cin >> anykey;
start();
}
void controls()
{
cout << "Throughout this game will you be making choices, each choice will have a corresponding number next to it. Press the letter to the corresponding choice you want to make. Press any key to continue\n";
cin >> anykey;
cout << "Here is an example...you are a cat, you can either\n\n";
cout << "1. Meow\n\n";
cout << "2. Hiss\n";
cout << "Press any key to continue\n";
cin >> anykey;
cout << "If I wanted to Hiss I would press the 2 key\n\n";
cout << "Press any key to continue on to the rules\n\n";
cin >> anykey;
rules();
}
void rules()
{
cout << "#1 You will be given a set number of choices. Press any key to continue\n\n";
cin >> anykey;
cout << "#2: There are 2 factors to this game. Press any key to continue\n\n";
cin >> anykey;
cout << "#3: The first factor is that either you live or you die. Press any key to continue\n\n";
cin >> anykey;
cout << "#4: The second factor is that if you live, your character will be judged based on events you've made. You can still live, but you might have a good or bad effect on the people around you. Press any key to continue\n\n";
cin >> anykey;
cout << "Let's play shall we?";
}
void start()
{
cout << "You shall begin a journey that'll decide either you make it or you don't. Press any key to continue\n\n";
cin >> fateNumber;
cout << "But before we start...The game needs to be explained.\n\n\n\n";
cout << "Controls(1 key)---------------------------Rules(2 key)---------------Skip(3 key) \n\n";
cin >> fateNumber;
if (fateNumber == 1)
{
controls();
}
else if (fateNumber == 2)
{
rules();
}
else if (fateNumber == 3)
{
fate();
}
else
{
cout << "invalid key\n\n\n\n\n\n\n";
start();
}
}
void fate()
{
cout << "Before we begin, a few characteristics must be selected\n\n";
cout << "Gender?\n\n";
cout << "1. Male\n";
cout << "2. Female\n";
cout << "3. Other\n\n";
cin >> genderNumber;
cout << "Now your next characteristic...Press any key to continue\n\n";
cin >> anykey;
cout << "What is your age? (Must be younger than 50 to play)\n\n";
cin >> ageNumber;
setting2();
if (genderNumber == 1)
{
string gender = "male";
}
else if (genderNumber == 2)
{
string gender = "female";
}
else if (genderNumber == 3)
{
string gender = "'it'";
}
else
{
"invalid input\n\n\n\n\n\n";
fate();
}
if (ageNumber >= 50 && ageNumber < 100)
{
cout << "Didn't you read the age rating dumbass? Younger than 50...\n\n";
}
else if (ageNumber < 50 && ageNumber > 22)
{
setting2();
}
else if (ageNumber <= 22 && ageNumber >= 16)
{
setting2();
}
else if (ageNumber < 16 && ageNumber >= 0)
{
setting2();
}
else if (ageNumber >= 100)
{
cout << "More than a century old and you expect me to believe you got this far? Bullshit.";
}
else
{
cout << "Error - Invalid number\n\n\n\n\n\n";
cout << "what is your age again?\n\n";
cin >> ageNumber;
}
}
void assessment()
{
cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
cout << "Press any key to continue\n\n";
cin >> anykey;
cout << "You are a " << ageNumber << " year old " << gender << ". You are " << setting << endl << endl;
if (settingNumber == 1)
{
string setting = "home";
}
else if (settingNumber == 2)
{
string setting = "jail";
}
}
void setting2()
{
cout << "Alright and lastly pick your setting\n\n";
cout << "1. Home sweet home\n";
cout << "2. Vacation \n";
cout << "3. Jail\n\n";
cin >> settingNumber;
assessment();
}
=======================================================================================
Any help is appreciated ^^ Code on!
Output (thru ofstream-s like cout) is buffered in C++. You want
cout << flush;
or
cout << endl;
to ask the buffer to be flushed (endl manipulator also emits a new line but flush does not).
So code:
cout << "You are a " << ageNumber << " year old " << gender << flush;
or
cout << "You are a " << ageNumber << " year old " << gender << endl;
Also, your gender should be declared at a wider scope:
if (genderNumber == 1) {
string gender = "male";
}
should become (to refer to the global gender)
if (genderNumber == 1) {
gender = "male";
}
BTW, if you enabled all warnings with your compiler (e.g. compile with g++ -g -Wall -Wextra, if using GCC, e.g. on Linux) you should have been warned. And of course, you should use the debugger (e.g. gdb, at least on Linux) and step into your code with it.
#include <iostream>
#include <iomanip>
using namespace std;
int main () // print to console: 3.0*5.0=15.00
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
return 0;
}
int calculate () // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
a = (7.1);
b = (8.3);
c = (2.2);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "- " << c << "\n" << endl;
cout << "------" << endl;
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
int calculation () // print to console: 3.2/(6.1*5.0)=0.10
{
double a;
double b;
double c;
a=(3.2);
b=(6.1);
c=(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << b << "*" << c << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
What does this output error mean? How do I fix it? someone please explain this to me. Am I suppose to add: int calculate(int a, int b, int c)?
Output:
(32): error C4716: 'calculate' : must return a value
You've declared your function as one that returns an int value but there's no return statement. Try changing the declaration to void calculate() if you don't need to return a value from it.
The calculate function needs to return a value.
You need to add something like this at the end of calculate:
return (a*b)-c;
It's only because your calculate function is supposed to return an int
and there's no return in your function.
if you don't want to return anything, you can put void calculate() instead