I am currently working on a program for a project, that asks for the user to enter the specific sport they want to play and their age for reservations at a recreation area. I am confused on how to store their sport and age into an array so that it can be displayed later in the program, if they select to view all reservations made by one or more users. If anyone could help me with figuring out how to store a single or multiple user input into an array so that it can be displayed later in the program that would be great!
#include <iostream>
#include <string>
using namespace std;
int main()
{
char t; // Type of sport selected
char g, G; // Gliding
char h, H; // Hang-gliding
char f, F; //Flying
int a; // Age of patron
double x; // Rates
int s; // Selection from menu
int i; // Arrays variable
int num;
char sport[100]; // Array for all sports of patrons
int age[100]; // Array for all ages of patrons
cout << "Please pick from the following menu" << endl;
cout << "1. Add a new reservation" << endl;
cout << "2. Print all reservations" << endl;
cout << "3. Print all reservations for a given sport" << endl;
cout << "4. Quit" << endl;
cin >> s;
for (i = 0; i < num; ++i)
if (s == 1) {
cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding" << endl;
cin >> t;
getline (cin, sport[i]);
cout << "Please enter the age of patron, minimum age is 16" << endl;
cin >> a;
if ((t == 'f' || t == 'F') && (a <= 25)) {
x = 68.95;
}
else if ((t == 'g' || t == 'G') && (a <= 25)) {
x = 73.95;
}
else if ((t == 'h' || t == 'H') && (a <= 25)) {
x = 99.95;
}
else if ((t == 'f' || t == 'F') && (a > 25)) {
x = 55.95;
}
else if ((t == 'g' || t == 'G') && (a > 25)) {
x = 65.95;
}
else if ((t == 'h' || t == 'H') && (a > 25)) {
x = 92.95;
}
cout << "The insurance rate is $ " << x << endl;
}
else if (s == 2) {
cout << "A patron aged " << a << " reserved a session of " << t << endl;
}
else if (s == 3) {
}
else if (s == 4);
return 0;
You should make a class Patron that contains the multiple informations, then make an array of type Patron instead of multiple arrays:
class Patron
{
//data for each patron...
};
in main:
Patron patrons[...];
You could also use dynamic containers, like vector instead of an array.
std::vector<Patron> patrons;
Related
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == ("Tesla" or "Volt" or "Leaf")) {
cout << "Electric" << endl;}
else {
if( n == ("Clarity" or "Mirai")){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
It needs to say how each automobile is powered. Basically if I input "Tesla", it should say "Electric"; "Ford" should come up "Gas Powered". It ends when I enter "End".
I get this error
.cpp|16|error: invalid operands to binary expression ('std::__1::string' (aka 'basic_string, allocator >') and 'bool')|
This should work! In C++ there is no or. Use ||. Also the condition needs to be put each time in if like if(n=="Tesla"||n=="Volt"||n=="Leaf").
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == "Tesla" || n== "Volt" ||n== "Leaf") {
cout << "Electric" << endl;}
else {
if( n == "Clarity" || n=="Mirai"){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
if( n == ("Tesla" or "Volt" or "Leaf"))
needs to be changed to
if( (n == "Tesla") || (n == "Volt") || (n == "Leaf") )
Make similar changes at the other places where you do the same thing
There is no or keyword in C++. You need to use ||
("Tesla || "Volt" || "Leaf") would always return true. So the condition will become if (n == (true)) which will never be true.
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);.
I'm having trouble trying to do this assignment for my class for a couple of days now and would like some help.
The assignment is to write a program that informs the user of their acceptance status based on their heigh and weight qualifications depending on their gender.
At the end of the program it wants to output the number of candidates that were accepted and the average of those accepted to the overall number of candidates.
Assignment - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf
We can't use switch, conditional operators, and selection (only for outputting the correct message to the results). We can only use loops and complex boolean expressions
The problems I'm having is:
If all 3 of my inputs are valid, why are they not outputting if they are accepted and if one of the input (height or weight) or both were rejected then why isn't it outputting it. Is my boolean variable incorrect? If so, how do I fix it.
Why am i unable to exit the loop/program when I enter X. Is my while loop correct or no?
Is there any selection statements I can change into "not selection-statements".
Here is my code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
float percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
while (gender != 'X' || gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = validCandidateCount / candidateCount;
cout << "That's " << percentOutput <<"%!" << endl;
return 0;
}
The main while loop should have and condition.
while(gender !='X' && gender!='x)
And your selection code has wrong conditional statements.
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK) // you have written else if(heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK) // you have written else if(weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
You should remove that invalidgender condition in the last do while loop, otherwise it will cause an infinite loop even if you wish to exit by pressing X.
Instead the invalid gender condition can be placed at the starting of main While loop.
And invalidGender variable should be assigned its value again otherwise it will pick up the previously stored value.
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
the whole code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
double percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
cout << "Gender: ";
cin.get(gender);
while (gender != 'X' && gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
}while(invalidGender);
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = (double)validCandidateCount / (double)candidateCount;
cout << "That's " << percentOutput*100 <<"%!" << endl;
return 0;
}
The Boolean data types need to be declared true or false. And this needs to be set before iterations.
Also:
The program needs to run before a user can input x to quit.
std:string QUIT;
QUIT = “XXX”;
Then use selection structure.
I am writing a "time travel" program which is supposed to ask the user for the current time, their target travel time, relay those values to be converted into minutes in a function and then based on if the time difference is too high it will not allow the time travel or if it is allowed it will print if they are in the future or the past. My issue right now is that the current time, which is only supposed to be relevant the first iteration, or the first "jump" of the program is not being updated after the jump occurs, and the program defaults to it instead of the target time, which is supposed to be the technical "current time" after the jump has occurred. I have been trying to figure this out for hours and I can't, so I am hoping someone could help me out.
Thank you for your time
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am);
void print_future();
void print_past();
void print_SecurityProtocol();
int main()
{
int c_hrs, c_mins;
int t_hrs, t_mins;
int system_time2;
int time_difference2;
bool c_am = 0;
bool t_am = 0;
char c, c2, Y, N, y, n, jumpAgain;
string am_or_pm_current, am_or_pm_target;
bool g_stop = true;
cout << "Welcome to Po Sled" << endl;
cout << "\tSystem booting..." << endl;
for (int i = 0; i<1; i++) //for loop to run once
{
cout << "\n\tEnter current time below: " << endl;
cout << "\t>Enter hour: "; //User inputs current time in hours
cin >> c_hrs;
while (c_hrs > 12 || c_hrs < 1)
{
cout << "Error: Please enter an hour in the range [1, 12]: ";
cin >> c_hrs;
}
cout << "\t>Enter minutes: "; //User inputs current time in minutes
cin >> c_mins;
while (c_mins > 59 || c_mins < 0) {
cout << "Error: Please enter minutes in the range [0, 59]: ";
cin >> c_mins;
}
cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM
cin >> am_or_pm_current;
while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") { //Checks if valid input, if not repeats message until valid
cout << "\tError: Please enter AM/am or PM/pm: ";
cin >> am_or_pm_current;
}
if ((am_or_pm_current == "am") || (am_or_pm_current == "AM"))
{
c_am = 1;
}
else if ((am_or_pm_current == "pm") || (am_or_pm_current == "PM"))
{
c_am = 0;
}
cout << "\n\tCurrent system time set to " << c_hrs << ":" << setw(2) << setfill('0') << c_mins << am_or_pm_current << endl;
cout << "\n\t\tIs this time correct (Y or N)? ";
cin >> c;
while (c != 'Y' && c != 'y' && c != 'n' && c != 'N')
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> c;
}
if (c == 'N' || c == 'n')
{
continue;
}
else
{
cout << "\n\tSystem initializing and TARDIS unit warming...." << endl;
cout << "\tThe Po Sled is engaged and ready for input." << endl;
}
}
do {
//Starts a loop for target jump
cout << "\n\tEnter target time below: " << endl; //Enters target time of jump
cout << "\t>Enter Hour: ";
cin >> t_hrs;
while (t_hrs > 12 || t_hrs < 1) {
cout << "Error: Please enter an hour in the range [1, 12]: ";
cin >> t_hrs;
}
cout << "\t>Enter minutes: ";
cin >> t_mins;
while (t_mins > 59 || t_mins < 0) {
cout << "\tError: Please enter minutes in the range [0, 59]: ";
cin >> t_mins;
}
cout << "\n\tIs it AM or PM?: ";
cin >> am_or_pm_target; //Classifying if target time is AM or PM
while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM")
{ //Validates input is AM or PM
cout << "\tError: Please enter AM/am or PM/pm: ";
cin >> am_or_pm_target;
}
if ((am_or_pm_target == "am") || (am_or_pm_target == "AM"))
{
t_am = 1;
}
else if ((am_or_pm_target == "pm") || (am_or_pm_target == "PM"))
{
t_am = 0;
}
cout << "\tTarget time set to " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target; //Sets target time
cout << "\n\t\tIs this time correct (Y or N)? "; //Validates if target time entered is correct
cin >> c2;
while (c2 != 'Y' && c2 != 'y' && c2 != 'n' && c2 != 'N')
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> c2;
}
time_difference2 = compute_time_difference(c_hrs, c_mins, c_am, t_hrs, t_mins, t_am);
if (time_difference2 > 360) //If time difference is greater than 6 hours prints error function
{
print_SecurityProtocol();
continue;
}
if (c2 == 'N' || c2 == 'n')
{
continue;
}
cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl;
if (time_difference2 < 0 && time_difference2 > -360) //If time difference is less than 0 prints past function
{
print_past();
}
else if (time_difference2 >= 0 && time_difference2 <= 360) //If time difference is ahead of current time prints future function
{
print_future();
}
cout << "\tWould you like to jump again (Y/N)? ";
cin >> jumpAgain;
while (jumpAgain != 'Y' && jumpAgain != 'y' && jumpAgain != 'n' && jumpAgain != 'N') //Input validation
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> jumpAgain;
}
if (jumpAgain == 'n' || jumpAgain == 'N') //User exiting program
{
if (time_difference2 < 0)
{
cout << "\t\tSystem shutting down; enjoy the past.\n" << endl;
}
else if (time_difference2 >= 0 && time_difference2 < 360)
{
cout << "\t\tSystem shutting down; enjoy the future.\n" << endl;
}
}
if (jumpAgain == 'Y' || jumpAgain == 'y')
{
continue;
}
} while (jumpAgain != 'n' && jumpAgain != 'N');
return 0;
}
int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am) //Computes time differences.
{
int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime;
int time_difference;
if (c_am == 1) //if c_am is true and it is morning
{
if (c_hrs == 12)
{
currentTime_hours = c_hrs * 0;
}
else if (c_hrs != 12)
{
currentTime_hours = (c_hrs * 60);
currentTime_minutes = c_mins;
currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time
}
}
else if (c_am == 0) //if c_am is false and it is afternoon time
{
if (currentTime_hours == 12)
{
c_hrs = c_hrs * 60;
}
else if (currentTime_hours != 12)
{
currentTime_hours = ((c_hrs + 12) * 60);
currentTime_minutes = c_mins;
currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time
}
}
if (t_am == 1) //if t_am is true and it is morning time
{
if (targetTime_hours == 12) //if target hours equal to 12 special math
{
targetTime_hours = t_hrs*0;
}
else if (targetTime_hours != 12) //else do this math
{
targetTime_hours = ((t_hrs) * 60);
targetTime_minutes = t_mins;
targetTime = targetTime_hours + targetTime_minutes;
}
}
else if (t_am == 0) //if target time equal to pm then do this math
{
if (targetTime_hours == 12)
{
targetTime_hours = t_hrs * 60;
}
else if (targetTime_hours != 12) //else if target time not equal to 12 then do normal pm math
{
targetTime_hours = ((t_hrs + 12) * 60);
targetTime_minutes = t_mins;
targetTime = targetTime_hours + targetTime_minutes;
}
}
time_difference = targetTime - currentTime;
cout << "the difference computed is " << time_difference;
return time_difference;
}
void print_SecurityProtocol() //Function which prints security protocol error message
{
cout << "\tSecurity Protocols Engaging" << endl;
cout << "\t\tError: The time difference is greater than 6 hours." << endl;
cout << "\t\t\tPlease re-enter target time." << endl;
}
void print_past() //Function that prints when a user is in the past
{
cout << "\tHold onto your lower posterior regions" << endl;
cout << "\n\t\tYou are now in the relative past" << endl;
}
void print_future() //Function that prints when a user is in the future
{
cout << "\tHold onto your lower posterior regions" << endl;
cout << "\n\t\tYou are now in the relative future " << endl;
}
Why do you not use a system time call, instead of getting user input for the 'current time'? Also, use a 24-hour clock instead of a 12-hour and you have removed the need to cater for am/pm within your process.
AM n ot much of a coder but c_hrs*60 would be (at least) 60 hours and (at most) 24*60 which, with the exception of a drive across Europe, Russia, Australia or the Northern US would be excessive time recording. Do you not mean to refer to MINUTES instead of hours (c_mins*60)?
I am a beginner and am stuck. I have written this and so far it is not working. After "Add or Remove Trader" it does nothing. Any help or tidbits on how to make this functional would be greatly appreciated. Thank you.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct Department{
string deptName;
int numTraders;
};
void addTraders(Department *, int );
void removeTraders(Department *, int);
int main(){
char addOrRemove;
Department departments[10] = {
{"Bank Loan", 10},
{"Conservative Allocation", 9},
{"Europe Stock", 10},
{"Domestic", 21},
{"Asia", 10},
{"Large Growth", 5},
{"Long-term Bond", 5},
{"Money Market", 25},
{"Emerging Market", 18},
{"Large Blend", 12}
};
int choice, numberToAdd, numberToRemove;
Department* p_departments = departments;
for(int i = 0; i < 10; i++){
cout << "Department # " << (i + 1) << ", Name: " << p_departments[i].deptName <<
", Traders: " << p_departments[i].numTraders << endl;
}
cout << endl;
do{
cout << "Enter 0 to quit, or choose a department number: ";
cin >> choice;
cout << "Add or remove traders (A or R) ? ";
cin >> addOrRemove;
if(addOrRemove == 'A' || 'a'){
cout << "how many traders to add" << endl;
cin >> numberToAdd;
addTraders(&departments[choice-1] ,numberToAdd);
}
else if(addOrRemove == 'R' || 'r'){
cout << "how many traders to remove" << endl;
cin >> numberToRemove;
removeTraders(&departments[choice-1],numberToRemove);
}
else{
cout << addOrRemove << " is not a valid selection. \n";
}
for(int i = 0; i < 10; i++){
cout << "Department # " << (i + 1) << ", Name: " << p_departments[i].deptName <<
", Traders: " << p_departments[i].numTraders << endl;
}
cout << endl;
}while(count != 0);
system("pause");
return 0;
}
void addTraders(Department *dept, int numAdd){
dept->numTraders += numAdd;
}
void removeTraders(Department *dept, int numRemove){
dept->numTraders += numRemove;
}
The following condition is always evaluated as true; even if it's false || 'a', 'a' ~> true:
if(addOrRemove == 'A' || 'a'){ ...
it was meant to be:
if(addOrRemove == 'A' || addOrRemove == 'a'){ ...
However when addOrRemove is declared as a char, then:
cin >> addOrRemove;
might just read a new-line character or some white space. It would be probably more reasonable to declare addOrRemove as std::string and change your condition to:
if(addOrRemove == "A" || addOrRemove == "a"){ ...
And after you read choice and it's 0, you should break your loop so that it won't try to access element at index 0 - 1:
cin >> choice;
if (choice == 0) break; // <-- THIS
First of all instead of
if(addOrRemove == 'A' || 'a'){
you should write
if(addOrRemove == 'A' || addOrRemove == 'a'){
And secondly you should define variable count because it seems that the compiler thinks that count - is name of standard algorithm std::count.