RPG Stats Issue - c++

I'm having a bit of an issue with my RPG stats code. I want people to use 6 basic stats (strength, dexterity, constitution, intelligence, wisdom, and charisma) that have a minimum value of 10. When creating the character, I'd like them to have 15 points to use, and in my code, it all works fine, unless you run out of points to place before you get to the last stat. Let's say you put all 15 points into strength. The display says that you'll have 25 strength, 32478493 dexterity, -42734627 constitution, -1 intelligence, etc. (these aren't exact number, just examples of what it looks like.) Here's the code.
CharCreate.h
#ifndef CharCreate_H
#define CharCreate_H
#include<fstream>
#include<string>
using namespace std;
int charcreate(){
fstream file;
char gender, choice;
string name, dummy;
int points;
int str, dex, con, intel, wis, cha;
float level;
double experience;
level = 1;
experience = 0;
ofstream myFile;
myFile.open ("T:\\character.txt");
system("color 2");
cout << "Welcome to the character creator." << endl;
genderchoice:cout << "First, are you male or female? (M/F)" << endl;
cin >> gender;
system("cls");
if (gender == 'M' || gender == 'm'){
cout << "You're male? (Y/N)" << endl;
cin >> choice;
system("cls");
if (choice == 'Y' || choice == 'y'){
cout << "Great!" << endl;
goto name;
} else if (choice == 'N' || choice == 'n'){
goto genderchoice;
}
} else if (gender == 'F' || gender == 'f'){
cout << "You're female? (Y/N)" << endl;
cin >> choice;
system("cls");
if (choice == 'Y' || choice == 'y'){
cout << "Great!" << endl;
goto name;
} else if (choice == 'N' || choice == 'n'){
goto genderchoice;
}
//------------------------------------------------------------------------------
name:system("cls");
system("color 3");
cout << "What is your name, traveler?" <<endl;
getline(cin,dummy);
getline(cin, name);
cout << "" << endl;
cout << "Your name is " << name << "? (Y/N)" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y'){
system("cls");
cout << "Greetings, " << name << "!" << endl;
} else if (choice == 'N' || choice == 'n'){
system("cls");
cout << "You must provide your name, stranger." << endl;
goto name;
}
//------------------------------------------------------------------------------
stats:system("cls");
system("color 4");
cout << "You have 6 stats to deal with in this game, and 15 points" << endl;
cout << "to allocate between them all." << endl;
cout << "These are: Strength (STR), Dexterity (DEX), Constitution (CON)," << endl;
cout << "Intelligence (INT), Wisdom (WIS), and Charisma (CHA)." << endl;
cout << "Continue: C" << endl;
cout << "Help: H" << endl;
cin >> choice;
if (choice == 'C' || choice == 'c'){
attrib:points = 15;
str:cout << "You have 10 Strength. How many more points do you wish to add?" << endl;
cin >> str;
points = points - str;
if (str > points > 15){
cout << "Not enough points!" << endl;
str = str - points;
goto str;
} else if (str == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto str;
} else if (str < points){
goto dex;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
str = str + 10;
cout << "You have " << str << " Strength" << endl;
system("pause");
system("cls");
dex:cout << "You have 10 Dexterity. How many more points do you wish to add?" << endl;
cin >> dex;
points = points - dex;
if (dex > points > 15){
cout << "Not enough points!" << endl;
dex = dex - points;
goto dex;
} else if (dex == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto dex;
} else if (dex < points){
goto con;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
dex = dex + 10;
cout << "You have " << dex << " Dexterity" << endl;
system("pause");
system("cls");
con:cout << "You have 10 Constitution. How many more points do you wish to add?" << endl;
cin >> con;
points = points - con;
if (con > points > 15){
cout << "Not enough points!" << endl;
con = con - points;
goto con;
} else if (con == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto con;
} else if (con < points){
goto intel;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
con = con + 10;
cout << "You have " << con << " Constitution" << endl;
system("pause");
system("cls");
intel:cout << "You have 10 Intelligence. How many more points do you wish to add?" << endl;
cin >> intel;
points = points - intel;
if (intel > points > 15){
cout << "Not enough points!" << endl;
intel = intel - points;
goto intel;
} else if (intel == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto intel;
} else if (intel < points){
goto wis;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
intel = intel + 10;
cout << "You have " << intel << " Intelligence" << endl;
system("pause");
system("cls");
wis:cout << "You have 10 Wisdom. How many more points do you wish to add?" << endl;
cin >> wis;
points = points - wis;
if (wis > points > 15){
cout << "Not enough points!" << endl;
wis = wis - points;
goto wis;
} else if (wis == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto wis;
} else if (con < points){
goto cha;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
wis = wis + 10;
cout << "You have " << wis << " Wisdom" << endl;
system("pause");
system("cls");
cha:cout << "You have 10 Charisma. How many more points do you wish to add?" << endl;
cin >> cha;
points = points - cha;
if (cha > points == 15){
cout << "Not enough points!" << endl;
cha = cha - points;
goto cha;
} else if (cha == points){
cout << "Are you sure you want to put all of your points here?" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
goto fin;
} else if (choice == 'N' || choice == 'n'){
goto cha;
} else if (con < points){
goto fin;
}
}
cout << "Remaining points: " << points;
cout << "" << endl;
cha = cha + 10;
cout << "You have " << cha << " Charisma." << endl;
system("pause");
system("cls");
fin:cout << "Your stats are:" << endl;
cout << "Strength: " << str << endl;
cout << "Dexterity: " << dex << endl;
cout << "Constitution: " << con << endl;
cout << "Intelligence: " << intel << endl;
cout << "Wisdom: " << wis << endl;
cout << "Charisma: " << cha << endl;
cout << "Are these correct? (Y/N)" << endl;
cin >> choice;
cout << "" << endl;
if (choice == 'Y' || choice == 'y'){
cout << "Congratulations, you have successfully finished your character." << endl;
} else if (choice == 'N' || choice == 'n')
goto attrib;
}
} else if (choice == 'H' || choice == 'h'){
cout << "Strength is how easily you can crush a tomato." << endl;
cout << "Dexterity is how easily you can handle a tomato with your hands." << endl;
cout << "Constitution is how easily you can eat a bad tomato." << endl;
cout << "Intelligence is knowing that tomato is a fruit." << endl;
cout << "Wisdom is not putting tomato in a fruit salad." << endl;
cout << "Charisma is selling a tomato-based fruit salad." << endl;
system("pause");
goto stats;
}
myFile << "Name: " << name << "\n";
myFile << "Gender: " << gender << "\n";
myFile << "\n";
myFile << "Level: " << level << "\n";
myFile << "Experience: " << experience << "\n";
myFile << "\n";
myFile << "Strength: " << str << "\n";
myFile << "Dexterity: " << dex << "\n";
myFile << "Constitution: " << con << "\n";
myFile << "Intelligence: " << intel << "\n";
myFile << "Wisdom: " << wis << "\n";
myFile << "Charisma: " << cha << "\n";
myFile.close();
}
#endif
main.cpp
#include <cstdlib>
#include <iostream>
#include "CharCreate.h"
using namespace std;
int main(int argc, char *argv[])
{
charcreate();
system("PAUSE");
return EXIT_SUCCESS;
}
How would I correct the problem of the numbers going haywire after running out of points? If it helps, I'm running Bloodshed Dev C++ as the compiler because that's the one we have to use at college.

In C++ if you don't set a value to a variable, it will contain whatever random data happens to be there at the time. You are going to want to initialize each stat to 10 (or some sane default value) at the start.

Related

I am only getting the last input from test() when I run the getStatistics() function

My C++ program wants the user to choose a test option. Option A wants the user to login their id, then they will proceed to take the test. Option B gives the user the results of the taken test. Finally, Option C will make the user quit.
I am trying to get the function getStatistics() to display whether the question that what the user answered is right or wrong, display the user answers and the correct answer.
`
#include <iostream>
#include <string>
# include <cctype>
using namespace std;
const int SIZE = 8;
int totalRightScore{};
int result = totalRightScore;
char ch;
char answer[SIZE] = { 'a', 'b', 'c', 'd' , 'A' , 'B' , 'C' , 'D' };
void test() {
char choice;
cout << "1. What is the capital of USA?" << endl;
cout << "(a)Georgia" << endl;
cout << "(b)Washington, Dc" << endl;
cout << "(c)Berlin" << endl;
cout << "(d)New York City" << endl;
cin >> ch;
if (ch == answer[1] || ch == answer[5]) {
totalRightScore = totalRightScore + 1;
}
cout << "2. How many stars has the USA flag have" << endl;
cout << "(a)50" << endl;
cout << "(b)60" << endl;
cout << "(c)35" << endl;
cout << "(d)13" << endl;
cin >> ch;
if (ch == answer[0] || ch == answer[4]) {
totalRightScore = totalRightScore + 1;
}
cout << "3. What is the name of your SQL instructor?" << endl;
cout << "(a)Alan Anderson" << endl;
cout << "(b)Dr House " << endl;
cout << "(c)Bill Nye" << endl;
cout << "(d)Dr Gill" << endl;
cin >> ch;
if (ch == answer[0] || ch == answer[4]) {
totalRightScore = totalRightScore + 1;
}
}
void loginid() {
string login = "test123";
cout << "enter your login id" << endl; ;
cin >> login;
if (login == "test123") {
test();
}
else
{
cout << "Sorry wrong user ID";
}
}
void getStatistics() {
cout << " Question 1 ";
if (ch == answer[1] || ch == answer[5]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(b) Washington, Dc" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(b)Washington, Dc" << endl;
}
cout << " Question 2 ";
if (ch == answer[0] || ch == answer[4]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a) 50" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a) 50" << endl;
}
cout << " Question 3 ";
if (ch == answer[0] || ch == answer[4]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a)Alan Anderson" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a)Alan Anderson" << endl;
}
result = totalRightScore;
cout << "your total score is " << result << "/3 " << endl;
totalRightScore = 0;
}
void exit() {
char choice;
cout << "Press Q or q to exit program." << endl;
cin >> choice;
if (choice == 'Q'|| choice == 'q')
{
cout << "Good bye Now" << endl;
}
}
void menu() {
string keepgoing = "y";
char choice;
while (keepgoing == "y")
{
cout << " Select Your Option:\n ";
cout << "(a Login with your userID\n ";
cout << "(b Statistics\n ";
cout << "(c Quit\n ";
cin >> choice;
switch (choice)
{
case 'a':
loginid();
break;
case 'b':
getStatistics();
break;
case 'c':
exit();
break;
}
cout << "would you like to continue? type 'y' to continue " << endl;
cin >> keepgoing;
}
}
`
The problem is that getStatistics() is just getting the last input from test(), for example if you input the right answer for question 1 and input the right answer question 3, when you run the getStatistics()it only remembers the last input from test() and thus deem the right answer for question 1 incorrect. Is there anyway for the getStatistics() to remember all the user answers for all the questions from test(), instead of just getting what is last answered.Thank you.

Nested IF Statement C++ Syntax

Is my syntax correct for this nested if statement?
I am trying to run through all values. But it does not seem to provide any outputs is all values are not equal to 1.
I am using Xcode so some of the code may differ than in VS.
#include <iostream>
using namespace std;
int main() {
int haveMoney, haveTime, amHungry, restaurantOpen, haveTransportation;
cout << "Yes = 1, 2 = No" << endl << endl;
cout << "Do I have money?" << endl;
cin >> haveMoney;
cout << "Do I have time?" << endl;
cin >> haveTime;
cout << "Am I hungry?" << endl;
cin >> amHungry;
cout << "Are they open?" << endl;
cin >> restaurantOpen;
cout << "Do I have transportation?"<< endl;
cin >> haveTransportation;
if ((haveMoney == 1) && (haveTime == 1) && (amHungry == 1) && (restaurantOpen == 1) && (haveTransportation == 1)){
cout << "Enjoy your McDonalds!" << endl << endl;
if (haveMoney == 2){
cout << "You're broke, so you can't have McDonalds" << endl ;
if (haveTime == 2){
cout << "You don't have enough time to go to McDonalds!" << endl ;
if (amHungry == 2){
cout << "Why are you even thinking about McDonalds, you're not hungry!" << endl ;
if (restaurantOpen == 2){
cout << "McDonalds is closed, tough luck." << endl ;
if (haveTransportation == 2){
cout << "You have no transportation to get to McDonalds." << endl ;
}
}
}
}
}
}
return 0;
}
You need chained if-else-if statements, not nested if statements. This does what you expect. Note that I have omitted the curly braces for the if statements for ease of typing.
#include <iostream>
using namespace std;
int main() {
int haveMoney, haveTime, amHungry, restaurantOpen, haveTransportation;
cout << "Yes = 1, 2 = No" << endl << endl;
cout << "Do I have money?" << endl;
cin >> haveMoney;
cout << "Do I have time?" << endl;
cin >> haveTime;
cout << "Am I hungry?" << endl;
cin >> amHungry;
cout << "Are they open?" << endl;
cin >> restaurantOpen;
cout << "Do I have transportation?"<< endl;
cin >> haveTransportation;
if ((haveMoney == 1) && (haveTime == 1) && (amHungry == 1) && (restaurantOpen == 1) && (haveTransportation == 1))
cout << "Enjoy your McDonalds!" << endl << endl;
else if (haveMoney == 2)
cout << "You're broke, so you can't have McDonalds" << endl ;
else if (haveTime == 2)
cout << "You don't have enough time to go to McDonalds!" << endl ;
else if (amHungry == 2)
cout << "Why are you even thinking about McDonalds, you're not hungry!" << endl ;
else if (restaurantOpen == 2)
cout << "McDonalds is closed, tough luck." << endl ;
else if (haveTransportation == 2)
cout << "You have no transportation to get to McDonalds." << endl ;
return 0;
}

How to add a text file to a game so that it can track a person's score

I have written a code that displays a rock-paper-scissors game against the computer. I would like to add a feature where I can create a text file in order to store the person's score and the computer score and keep track of the score but I don't know how to do it. Thank you in advance!
Here is my code.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void rock_paper_scissors()
{
static int userscore = 0;
static int computerscore = 0;
string playername;
int userchoice;
int computerchoice;
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
cout << "Please enter your choice\n";
cin >> userchoice;
cout << endl;
while (!(userchoice > 0 && userchoice <= 3))
{
cout << "invalid choice. please enter a number between 1 and 3\n";
cin >> userchoice;
}
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
}
else
{
cout << "computer wins!" << endl;
}
cout << "thank you for playing!\n";
string restart;
cout << "Would you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
if (restart == "y")
{
rock_paper_scissors();
}
}
int main()
{
cout << "MAIN\n";
rock_paper_scissors();
return 0;
}
Why do you need to write the data into in file? Updating a simple txt file after each round could be cumbersome, as you always result in a single score at the end of the program. I would suggest changing the type of the rock_paper_scissors into int indicating the score achieved by the player in a single lot. The intermediate results are irrelevant. Just place the game loop into your main function and do not use a recursive function call and static function variables here. Otherwise, the player is forced to enter his name for every single lot.
Moreover, I tested your code and you have to change your error handling. I typed in "rock" and the program stuck in an infinity loop "invalid choice. please enter a number between 1 and 3\n" Whereby it is not possible to make another entry. As I entered a string instead of an integer, you have to reset the console. Beware of the dumbest possible user.
Moreover, you should seed your program to avoid identical computer choices in each game. This could be done with srand(time(NULL)).
Eventually, I write the tracked score into a score file at the end of the main function using the fstream standard library.
#include <iostream>
#include <string>
#include <algorithm> // min max
#include <fstream> // read/write from/to files
#include <time.h> // time
using namespace std;
int rock_paper_scissors(const std::string& playername);
int main()
{
srand(time(NULL));
cout << "MAIN\n";
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
int userscore = 0;
int computerscore = 0;
std::string playername;
std::string restart;
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
do
{
int result = rock_paper_scissors(playername);
cout << "thank you for playing!\n";
userscore += result;
computerscore += std::max(0, 3 - 2 * result);
cout << playername << "'s score: " << userscore;
cout << "\ncomputer's score: " << computerscore;
cout << "\nWould you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
} while (restart == "y");
std::ofstream ofile;
ofile.open("scorefile.txt");
ofile << "Scores:\n" << playername << ": " << userscore;
ofile << "\nComputer: " << computerscore;
ofile.close();
return 0;
}
int rock_paper_scissors(const std::string& playername)
{
int userchoice;
int computerchoice;
cout << endl << endl;
do {
cout << "Please enter your choice\n";
if (std::cin >> userchoice)
{
if (userchoice > 0 && userchoice <= 3)
{
break;
}
else
{
cout << "invalid choice. please enter a number between 1 and 3\n";
continue;
}
}
else if (!cin.bad() && !cin.eof())
{
// a non integer value entered
cerr << "invalid choice. please enter a number between 1 and 3\n";
// Reset error state
cin.clear();
// remove error input
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
} while (true);
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
return 1;
}
else
{
cout << "computer wins!" << endl;
return 0;
}
}

Price does not accumulate if user repeat order

First of all, sorry if this is a dumb question.
These are my 2 functions. I'm trying to make a repeated order in function listHardware as you can see, "Do you want to purchase more? Press y for yes." at the bottom of the function. But if user repeat order, only the new price will be displayed. Why does not it accumulate with the previous price of their previous order?
I thought I made it right by doing price+= in calcFunc.
void listHardware()
{
int i,j,type,m_type,c_type,r_type,s_type,g_type,quantity=0;
const int SIZE=3;
char shopmember,addorder = 'y';
double price=0;
while(addorder=='Y' || addorder=='y')
{
cout << endl << "Select which type of hardware that you want to purchase: ";
cin >> type;
if(type==1)
{
const char *monitor[SIZE][2]=
{
{"BenQ PD3200U", "(RM3000)"},
{"Acer Predator X34", "(RM4000)"},
{"Dell UltraSharp UP3218K", "(RM8000)"}
};
cout << "Monitors:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << monitor[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Monitor you would like to purchase: ";
cin >> m_type; //monitor
cout << endl << "How many Monitor would you like to purchase?" << endl;
cin >> quantity;
}
if(type==2)
{
const char *cpu[SIZE][2]=
{
{"AMD Ryzen 7 2700X", "(RM1200)"},
{"Intel Core i5-8600K", "(RM1200)"},
{"Intel Core i9-7980XE", "(RM8000)"}
};
cout << "CPU:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << cpu[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which CPU would you like to purchase: ";
cin >> c_type; //cpu
cout << endl << "How many CPU would you like to purchase?" << endl;
cin >> quantity;
}
if(type==3)
{
const char *ram[SIZE][2]=
{
{"Patriot Viper Elite 8GB DDR4-2400MHz", "(RM400)"},
{"G.Skill Ripjaws V 16GB DDR4-2400MHz", "(RM1200)"},
{"Corsair Dominator Platinum 32GB DDR4-3333MHz", "(RM2000)"}
};
cout << "RAM:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ram[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which RAM would you like to purchase: ";
cin >> r_type; //ram
cout << endl << "How many RAM would you like to purchase?" << endl;
cin >> quantity;
}
if(type==4)
{
const char *ssd[SIZE][2]=
{
{"Samsung 860 Pro 1TB", "(RM1250)"},
{"Crucial MX500 1TB", "(RM600)"},
{"WD Blue 2TB", "(RM1600)"}
};
cout << "SSD:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << ssd[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which SSD would you like to purchase: ";
cin >> s_type; //ssd
cout << endl << "How many SSD would you like to purchase?" << endl;
cin >> quantity;
}
if(type==5)
{
const char *gcard[SIZE][2]=
{
{"Nvidia GeForce RTX 2080 Ti", "(RM4000)"},
{"Nvidia GeForce GTX 1080 Ti", "(RM2900)"},
{"AMD Radeon RX 580 8GB", "(RM2100)"}
};
cout << "Graphic Card:" << endl;
for(i=0;i<SIZE;i++)
{
cout << "\t" << i+1 << ". ";
for(j=0;j<2;j++)
{
cout << gcard[i][j] << " ";
}
cout << endl;
}
cout << endl << "Enter which Graphic Card would you like to purchase: ";
cin >> g_type; //gpu
cout << endl << "How many Graphic Card would you like to purchase?" << endl;
cin >> quantity;
}
cout << "Membership (Y/N): ";
cin >> shopmember;
cout << "Do you want to purchase more? Press y for yes." << endl;
cin >> addorder;
cin.ignore();
price=calcFunc(m_type,c_type,r_type,s_type,g_type,quantity,price,shopmember); //function call
}
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "Total Price: " << price << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
}
//
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
{
const double discount=0.1;
if(m_type==1) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(3000-(3000*discount))*quantity;
else
price+=3000*quantity;
}
else if(m_type==2) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(m_type==3) //monitor
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(c_type==1) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==2) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(c_type==3) //cpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(8000-(8000*discount))*quantity;
else
price+=8000*quantity;
}
if(r_type==1) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(400-(400*discount))*quantity;
else
price+=400*quantity;
}
else if(r_type==2) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1200-(1200*discount))*quantity;
else
price+=1200*quantity;
}
else if(r_type==3) //ram
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2000-(2000*discount))*quantity;
else
price+=2000*quantity;
}
if(s_type==1) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1250-(1250*discount))*quantity;
else
price+=1250*quantity;
}
else if(s_type==2) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(600-(600*discount))*quantity;
else
price+=600*quantity;
}
else if(s_type==3) //ssd
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(1600-(1600*discount))*quantity;
else
price+=1600*quantity;
}
if(g_type==1) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(4000-(4000*discount))*quantity;
else
price+=4000*quantity;
}
else if(g_type==2) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2900-(2900*discount))*quantity;
else
price+=2900*quantity;
}
else if(g_type==3) //gpu
{
if (shopmember == 'y' || shopmember == 'Y')
price+=(2100-(2100*discount))*quantity;
else
price+=2100*quantity;
}
}
double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
Is missing a return statement.
Add
return price;
at the end of the function and you should be good.

C++ my program reads backspace as a character

I am working with a c++ program, but I am stuck with annoying bug. The bug is that when i type the password, it counts backspace as a character so can I fix it? Here is the code.
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
string password, username, lon, nu, np;
char c;
int StarNum = 0, humanproof;
cout << "Do you wanna create a user or login?";
cout << "\nLogin" << endl;
cout << "New" << endl;
cin >> lon;
if(lon=="login"){
goto login;
}
if(lon=="new"){
goto newa;
}
login:
cout << "Username: ";
cin >> username;
lol:
cout << "Password: ";
while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
password += c;
cout << "*";
if (c == 127 || c == 8){
//go here to fix the problem
}
password = "";
goto lol;
}
}
if(username == "user" && password == "pass" || username == nu && password == np){
cout << "\nYou are logged in.";
goto options;
} else {
cout << "\nusername or password is wrong" << endl;
return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
cout << "The username is " << nu << endl;
for(int no = 0; no <= 100; no++){
cout << endl;
}
goto login;
return 0;
}
if(humanproof!=423){
cout << "wrong answer!";
return 0;
}
options:
int op;
cout << "\nwhat do you want to do?" << endl;
cout << "1. Calculator" << endl;
cout << "2. About"<< endl;
cout << "3. Just for fun" << endl;
cout << "4. Exit" << endl;
cin >> op;
if(op==1){
goto calculator;
}
if(op==2){
goto info;
}
if(op==3){
goto fun;
}
if(op==4){
return 0;
}
else{
cout << "you entered a invalid number. " << endl;
return 0;
}
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 + n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 2){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 - n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 3){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 * n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 4){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 / n2;
cout << "the sum is " << sum;
return 0;
}
if(opa > 4){
cout << "You entered a invalid number";
goto calculator;
}
info:
cout << "Created by Bergur 2013";
return 0;
fun:
cout << "You want an eyepad(ipad)?";
}
Your code already checks:
while (c != 13)
And prevent newline from being handled, do similar to what you need with the backspace character, whose number is 8
To fix your issue, before:
StarNum++;
Add:
if (c == 8 && StarNum > 0) {
StarNum--;
if (password.size () > 0)
password.resize (password.size () - 1);
continue;
}
Please format your code. Also, try to provide a minimal code which reproduce the problem, not the whole code.
Unrelated to your problem
Try not to use goto.
Do not use ASCII values, but use the char literals instead, i.e. use '\n' instead of 13.
Also, you can simply add an iother condition inside your while:
while (c != '\n' && c != ' ')