C++ How to Increment Int Variable From String Array Element - arrays

I'm wondering if there is a way to increment an int variable when a specific string array element is called? I'm working on a project for fun and have run into a roadblock. The goal of my project is to have a program that determines what genres of electronic music the user likes. The program will present two DJ's and ask the user which one they prefer or allow them to select neither. Each DJ will have up to three genres that they mainly specialize in, and I have created int variables for each (with all being set to 0). Once a user selects a DJ, I want points to be assigned to each genre variable in which the DJ is associated with. I'm unsure of how to set up this rule as everything I have attempted so far has failed (two example attempts are commented out in the code). Eventually my plan is to develop logic to have the DJ's randomly selected, however I need to have the genre tally allocation set up first. Is there any way this can be done? Any help would be greatly appreciated. Cheers!
###include "stdafx.h"
###include < iostream>
###include < iomanip>
###include < string>
###include < array>
###using namespace std;
int main()
{
cout << "Hello! This program is designed to figure out what Electronic Music you like based on artists presented and the answers you choose...\n" << endl;
cout << "When you are ready to begin press \"Enter\"..." << endl;
getchar();
int bigRoom = 0;
int deepHouse = 0;
int drumBass = 0;
int dubstep = 0;
int electroHouse = 0;
int futureHouse = 0;
int hardDance = 0;
int house = 0;
int progressiveHouse = 0;
int techno = 0;
int trance = 0;
int trap = 0;
string textArray[5]{ "DeadMau5", "Armin Van Buuren", "Avicii", "Ferry Corsten", "Kaskade"};
string answer;
cout << "Select the DJ you prefer by number. Otherwise select 3 if you don't know them. " << endl; //Haven't coded option 3 yet.
cout << "1 - " << textArray[1] << endl;
cout << "2 - " << textArray[2] << endl;
cin >> answer;
/*
if (textArray[1]) {
++trance;
}
for (textArray[1]) {
++trance;
}
*/
if (answer == "1") {
cout << "You have selected: " << textArray[1] << endl;
}
else if (answer == "2") {
cout << "You have selected: " << textArray[2] << endl;
}
//cout << trance << endl;
}

You can increment each tally once the user has selected a DJ:
if (answer == "1") {
cout << "You have selected: " << textArray[1] << endl;
++trance;
// ++ ohter genres you want to increment
}
else if (answer == "2") {
cout << "You have selected: " << textArray[2] << endl;
++trance;
// ++ ohter genres you want to increment
}

Related

c++ Getting the Summation of numbers using an array and pointers

So I was doing a program where i need to find the total area of all floors, where the floors are determined by the user. I think I'm correct on the part to use the pointers, so to check if the code was correct I tried it doing it with basic addition. but even with basic addition it seems to have already some problems. I tried looking for similar questions in here and I can't find anything that might help me, so I hope you guys can help me. Thank you in advance.
float *Length=NULL, *Width=NULL, *Area=NULL, TotalArea, templ, tempw;
int floors, count;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
for (int loop = 0; loop < floors; loop++)
{
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length + loop) = templ;
*(Width + loop) = tempw;
*(Area + loop) = (*Length + loop) + (*Width + loop);
count = loop;
for (int count = 0; count < floors; count++)
{
TotalArea = TotalArea + *Area+count;
}
}
cout << TotalArea << endl;
I tried inputting the following:
floor:2
floor 1
length: 1
width: 1
floor 2
length: 1
width: 1
The answer should be 4, but the output ends up with 10.
You don't need arrays and pointers. Since you are looping through the floors and summing the areas of each floor, you can "forget" about previously encountered lengths, widths and areas, and only remember the total area encountered so far.
float length;
float width;
float area;
float totalArea;
int floors;
std::cout << "Input the number of floors to proceed\n";
std::cout << ":";
std::cin >> floors;
totalArea = 0;
for (int loop = 0; loop < floors; loop++)
{
std::cout << "\n\nFloor " << loop + 1 << std::endl;
std::cout << "Input the Length: " << std::endl;
std::cin >> length;
std::cout << "Input the Width: " << std::endl;
std::cin >> width;
area = length * width;
totalArea += area;
}
std::cout << totalArea << std::endl;
Notice how I added these annoying std:: everywhere? If you wonder why, see this other question: Why is “using namespace std;” considered bad practice?
I tried making some changes based on you guys comments and here is what I did.
There are some warnings I don't really know why but It seems to be working
float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;
int floors, loop = 0;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
for (int loop = 0; loop < floors; loop++)
{
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length +loop) = l;
*(Width+loop) = w;
Area[floors] = (*(Length+loop)) * (*(Width+loop));
TotalArea += a[floors];
}
cout << TotalArea << endl;
This is what I came u with, it seems to be working, the output is also correct now.

check order of array in c, using recursion and malloc [duplicate]

I am trying to debug a recursive function used to validate user input and return a value when the input is OK. The function looks like this:
double load_price()
{
double price;
Goods * tempGd = new Goods();
cin >> price;
while (!cin)
{
cin.clear();
#undef max
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << endl;
cout << "You didn't enter a number. Do so, please: ";
cin >> price;
} // endwhile
if (!tempGd->set_price(price))
{
cout << endl;
cout << "The price " << red << "must not" << white << " be negative." << endl;
cout << "Please, insert a new price: ";
load_price();
}
else
{
delete tempGd;
return price;
}
}
The method set_price() of Goods class looks as follows
bool Goods::set_price(double price)
{
if (price> 0)
{
priceSingle_ = price;
priceTotal_ = price* amount_;
return true;
}
return false;
}
I tried drawing the problem on a paper but all my diagrams seem to look the way my function already looks like. I think there are some problems with returns, but I do not know where.
Help would be greatly appreciated.
You're not using the return value of the recursive call. You need to do:
return load_price();
Who talked you into using recursion for that problem?
#undef max
double load_price()
{
for(;;) {
double price;
cin >> price;
if (!cin)
{
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << endl;
cout << "You didn't enter a number. Do so, please: ";
continue;
}
if (!Goods().set_price(price))
{
cout << endl;
cout << "The price " << red << "must not" << white << " be negative." << endl;
cout << "Please, insert a new price: ";
continue;
}
return price;
}
}

how do i pick a memory in an array that i create after displaying the array value. how to slect thte value with cin when the program run

string bookname [5] = {"STPM BOOKS","SPM BOOKS","PMR BOOKS","TEXT BOOK","PRACTICAL BOOK"};
float price [5] = {30.90,24.90,19.90,45.90,15.90};
for(int i = 0; i < 5 ; i++)
{
cout << i+1 << "book name " << bookname[i] << " ---- RM " << price[i] << endl;}
i want to select the value for example like stpm books and its price from the array and display it to cout after selecting from a cin and doing the selection when the code was excercuted
}
Based on your updated query, asking for an exit condition, I have modified the code. Please note that I have used getline(cin, answer) to handle the input. I use getline() because I am less likely to mess up when I use getline() over cin >> answer.
Note that getline(cin, answer) does a few things for you:
Returns true if input was received
Fills in the answer
Gets an entire line, until user provides new line
Thus, the line while (std::getline(std::cin, answer) && answer != "n") will get an entire book, even if there are spaces. The && allows for a check of the answer for "n" before entering the while loop's body.
As answer is a std::string, the comparison is to "n", not 'n'.
#include <iostream>
#include <string>
#include <iomanip> // setprecision
int main()
{
std::string bookname[5] =
{"STPM BOOKS", "SPM BOOKS", "PMR BOOKS", "TEXT BOOK", "PRACTICAL BOOK"};
float price[5] = {30.90, 24.90, 19.90, 45.90, 15.90};
std::string answer;
float cost;
std::cout << "Please provide the name of a book or type 'n' to exit"
<< std::endl;
// get one line into answer, and also exit while if answer is "n"
while (std::getline(std::cin, answer) && answer != "n") {
bool found = false;
for (int i = 0; i < 5; i++) {
if (bookname[i] == answer) {
std::cout << "The book " << bookname[i] << " costs $" << std::fixed
<< std::setprecision(2) << price[i] << std::endl;
found = true;
break; // /no need to continue inside the for loop
}
}
// on last loop, give up
if (!found) {
std::cout << "The book " << answer << " is not in our inventory"
<< std::endl;
}
}
}

How do I deal from a deck array to a player hand array without having duplicates in c++?

maybe i missed this being answered in another question but after searching i can't seem to find the answer i need.
here is the code i am using to deal cards as it stands which correctly changes the first two card values to random ints from the deck array. however as it is written, the code occasionally passes duplicate values into the player hand.
void Deal()
{
for (int i = 0; i < 2; ++i)
{
playerHand[i] = deck[i];
dealerHand[i] = deck[i + 2];
if (playerHand[i] == 11)
{
cout << "BLACKJACK!! YOU WIN!!" << endl;
WinHand();
}
else if (dealerHand[i] == 11)
{
cout << "Dealer Got Blackjack. Hand goes to dealer." << endl;
}
else
{
playerNextCard++;
dealerNextCard++;
deckNextCard = 4;
}
}
for (size_t i = 0; i < 2; i++)
{
cout << playerHand[i] << endl;
}
cout << "? \n" << dealerHand[1] << endl;
cout << "Money: " << money << endl;
return;
}
The Player, and Dealer are always receiving the first four cards unless an 11 is drawn where it terminates early. Whether, or not the deck is shuffled there is a possibility to receive a duplicate integer. If you want to make each draw integer unique then you can search the deck until the previous one does not match. You will also have to consider what happens if no unique integer is found.

Issue with FOR loops and IF statement

Trying to display # of each combo ordered and total price. Not sure why it won't store values in A, B, and C. Novice programmer here, so be easy. Been having the problem with if statements for a while, so obviously I'm doing the whole if statement thing incorrectly.
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
int A = 0;
int B = 0;
int C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter = counter + 1)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo = A)
{
A = A + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = B)
{
B = B + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = C)
{
C = C + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
}
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
For-loop should be for (int counter = 0; counter < group; counter++)
If statement should use == for equality. = is for assignment only.
Your if statement needs to quote the character you are comparing: if (combo == 'A') {. It is also possible that you'll need to access combo as an array of characters, like so: if (combo[0] == 'A') {
I think you may need to tweak just a few things; a few compilers don't like it when you compute a double using int and there is no reason to not use double here since the program is small. Also a few syntax errors (i.e. with = instead of ==). Have you tried displaying your output in an isolated way? Something like:
main(){
double A = 1;
double B = 2;
double C = 3;
double total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
Your corrected code:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
double A = 0;
double B = 0;
double C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo == 'A')
{
A++;
}
else if (combo == 'B')
{
B++;
}
else if (combo == 'C')
{
C++;
}
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
I would also display your value for group to make sure that the if loop is even running at least once. There are a few fault points here that I would test individually.
EDIT:
Maybe test this:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Test success";
}
to see if you are even entering into your for loop.

Resources