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.
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.
Basically, this program allows a user to enter a sentence and depending on the users selection, it will show the middle character of the sentence, display it uppercase or lowercase, or backwards. Simple program, but I am new to programming so that may be the problem. I would like to figure out how to use loops instead of a ton of if statements. When I try to make some loops it breaks certain parts of the code but I am sure that is because I don't properly understand them. If you have any criticism or any advice on the code, I'd be happy to hear it. Thanks in advance!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sel;
string sent;
bool validinput;
int i;
int x;
int j;
int a;
cout << "Welcome to my program. Enter a sentence and select one of the options below.\n";
cout << "Enter -999 to exit the program." << endl;
cout << "============================================================================" << endl;
cout << endl;
cout << "1. Display the middle character if there is one." << endl;
cout << "2. Convert to uppercase." << endl;
cout << "3. Convert to lowercase." << endl;
cout << "4. Display backwards." << endl;
cout << "Enter a sentence: ";
getline (cin, sent);
cout << "Selection: ";
cin >> sel;
if (sel < 1 && sel > 4)
{
cout << "Invalid input. Try again. Selection: ";
cin >> sel;
validinput = false;
}
else (sel >= 1 && sel <= 4);
{
validinput = true;
}
if (validinput == true)
{
if (sel == 1)
{
j = sent.length() / 2;
cout << "The middle character is: " << sent.at(j) << endl;
}
if (sel == 2)
{
for (int i = 0; i < sent.length(); i++)
{
if (sent.at(i) >= 'a' && sent.at(i) <= 'z')
{
sent.at(i) = sent.at(i) - 'a' + 'A';
}
}
cout << "Uppercase: " << sent << endl;
}
if (sel == 3)
{
for (int x = 0; x < sent.length(); x++)
{
if (sent.at(x) >= 'A' && sent.at(x) <= 'Z')
{
sent.at(x) = sent.at(x) - 'A' + 'a';
}
}
cout << "Lowercase: " << sent << endl;
}
if (sel == 4)
{
for (a = sent.length() - 1; a >= 0; a--)
{
cout << sent.at(a);
}
}
}
system("pause");
return 0;
}
Personally I would use the switch selection statement. I roughly did this just to explain a bit on how it can make your code more friendly and understandable.
int sel;
bool validInput = false;
switch(sel)
{
case 1:
//display middle char if there's one
case 2:
//convert to uppercase
case 3:
//convert to lowercase
case 4:
//display backwards
validInput = true;
break;
default: //if number does not meat 1, 2, 3 or 4
validInput = false;
break;
}
As you may notice, for case 1, case 2, case 3 and case 4, there's a break just to say that if the number is between 1 to 4; validInput is true.
Reference: Switch Selection Statement
i suggest using a switch. It will organize your code better. From looking at your code you seem to have used for and if wisely. But I suggest the if statements checking for the input be replaced with switch.
I am working on a school lab and i cant seem to figure out why the code keeps going in to a infinite loop. I know something is wrong with the while statement but i cant see what it is.
#include <iostream>
using namespace std;
int main()
{
int c = 0, p = 0, prof;
cout << "Do you like Coke or Pepsi? " << endl;
cin >> prof;
//put in data validation
while (prof != 'q' && prof != 'Q')
{
if (prof == 'p')
p++;
else if (prof == 'c')
c++;
cout << "Do you like Coke or Pepsi? " << endl;
cin >> prof;
}
if (p > c)
cout << "Pepsi Wins";
else if (p < c)
cout << "Coke Wins";
else
cout << " It's a tie";
system("Pause");
}
Simply the variable prof should be a char not int
char prof;
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;
I am encountering a logical error with this app. It is a word jumble app that displays a jumbled word and asks the player if he/she would like to play again once they guess correctly.
When I tell the app I do not want to play again it continues through the sequence anyway. I have a feeling that its bad nesting on my part.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Are you banging your head against something?"},
{"jumble", "Its what this game is all about."},
{"glasses", "You might need these to read this text."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
};
srand(static_cast<unsigned int>(time(0)));
cout << "\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the the letters to make the word!\n";
cout << "Enter 'hint' for a hint\n";
cout << "Enter 'quit' to quit the game\n\n";
const int MAX_LEVEL = NUM_WORDS - 1;
int totalScore = 0;
for (int level = 0; level <= MAX_LEVEL; ++level)
{
string theWord = WORDS[level][WORD]; // Word to guess
string theHint = WORDS[level][HINT]; // Word hint
char playAgain;
string jumble = theWord; //Jumbled version of the word
int length = jumble.size();
int score = jumble.size() * 10;
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << jumble << endl;
string guess;
cout << "\nYour Guess: ";
cin >> guess;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
score = score / 2;
}
else
{
cout << "\n\nSorry thats not it.\n\n";
}
cout << "\n\nYour Guess: \n\n";
cin >> guess;
}
if (guess == theWord)
{
cout << "Thats it! You guessed it!\tYou scored: " << score << "\n\n";
cout << "Would you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain = 'y')
{
continue;
}
else if (playAgain = 'n')
{
cout << "Your total score is: " << totalScore << endl;
break;
}
}
else if (guess == "quit")
{
if (totalScore > 0)
{
cout << "Your total score is: " << totalScore << endl;
}
break;
}
}
cout << "\nGoodbye.";
return 0;
}
When comparing playAgain to 'y' and 'n', you only have one equals sign, causing the first one ('y') to always execute instead of it being an actual choice, since the value of 'y' is not 0.
To fix this, they should be:
if (playAgain == 'y') //note ==
{
continue;
}
else if (playAgain == 'n') //note ==
{
cout << "Your total score is: " << totalScore << endl;
break;
}
Also, any sane (more modern) compiler should warn you about this if you have warnings turned on. Be sure to turn those on and take heed of them.
I think you will need == for your playAgain question. I often make mistakes with that.