Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I developed this program to add and the retrieve 15$ in the stack.
I was wondering is there another more efficient way to write my code.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
const int MAX = 100;
int count;
stack<int, vector<int> > billStack;
for (int i=0; i<15; i++) {
billStack.push(i); // add 15 bills onto stack
}
cout << "The stack has " << billStack.size() << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
for (int i = 0; i< cash; i++) {
billStack.pop();
}
cout << "Cash out :" << cash << ". Remaining: " << billStack.size() << endl;
return 0;
}
Considering that you don't ever use the actual contents of the stack, just its size:
#include <iostream>
using namespace std;
int main()
{
int stackSize = 15;
cout << "The stack has " << stackSize << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
stackSize -= cash;
cout << "Cash out :" << cash << ". Remaining: " << stackSize << endl;
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
What's the diff between \n and endl? Is \n faster?
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter 2 numbers:" << endl;
int a = 0, b = 0;
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is " << a + b << endl;
return 0;
}
Yes, '\n' is faster here. std::endl will additionally call flush() which is not needed in this case.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
trying to take user input with getinput function,transfer it to vaidornot function and display if else loop to screen with main method but i couldnt. How can i do that ? also there is a problem with double variable ? :(
#include <iostream>
#include <string>
using namespace std;
void getinput(string restname, string name, double point) {
cout << name << "please enter the name of restaurant: " << endl;
cin >> restname;
cout << name << "please enter the scores of" << restname << "for first level evolution" << endl;
cin >> point;
}
void validornot(double point) {
if (point > 5) {
cout << "point is bigger than 5" << endl;
}
else {
cout << "point is smaller than 5" << endl;
}
}
int main() {
string name;
string restname;
double point;
cout << "Welcome to my restaurant rating programme.Please enter your name to start:" << endl;
cin >> name;
cout << "Welcome " << name << endl;
getinput(restname, name, point);
validornot(point);
return 0;
}
You can change void getinput(string restname,string name, double point)
to
void getinput(string restname,string name, double& point)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why does this code compile and work properly
#include <iostream>
int main()
{
using namespace std;
unsigned short int myInt = 99;
unsigned short int * pMark = 0;
cout << myInt << endl;
pMark = &myInt;
*pMark = 11;
cout << "*pMark:\t" << *pMark << "\nmyInt:\t" << myInt << endl;
return 0;
}
When this one doesn't compile:
#include<iostream>
using namespace std;
int addnumber(int *p, int *q){
cout << *p = 12 << endl;
cout << *q = 14 << endl;
}
int main()
{
int i , j;
cout << "enter the value of first number";
cin >> i;
cout << "enter the value of second number";
cin >> j;
addnumber(&i, &j);
cout << i << endl;
cout << j << endl;
}
In both the code snippets, I am assigning *pointer=somevalue. In the first code, it does not cause an error, but in the second, it causes error in the line
cout << *p = 12 << endl;
cout << *q = 14 << endl;
What mistake I am making?
Because of operator precedence.
To the compiler, your statement looks like
(cout << *p) = (12 << endl);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my program ( I will include code below ), I have a function to determine the user's name and height. I use the name function first void name() and then the function void height() following it (of course main is last).
What I'm trying to do is to display the user's name throughout the program. In my second function, void height() Is ask the user how tall they are:
cout << " How tall are you?" << endl;
I would like to ask " How tall are you, name1?" , but the string name1 is not declared in the scope. Any ideas of how to make it work / what I'm doing wrong? Thank you. Also if you see any other issues or something I can do to make things easier/alternative ways, please let me know! (I'm new!)
#include <iostream>
#include <string>
using namespace std;
void name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
}
void height()
{
//feet and inches to inches
cout << " How tall are you?" << name1 << endl;
cout << " " << endl;
cout << " " << endl;
cout << " Enter feet: ";
int feet;
cin >> feet;
cout << " " << endl;
cout << " Enter inches: ";
int inches;
cin >> inches;
int inchesheight;
inchesheight = (feet * 12) + inches;
cout << " " << endl;
cout << " Your height is equal to " << inchesheight << " inches total." << endl;
if (inchesheight < 65 )
{
cout << " You are shorter than the average male." << endl;
}
else if (inchesheight > 66 && inchesheight < 72)
{
cout << " You are of average height." << endl;
}
else
{
cout << " You are taller than average." << endl;
}
}
int main()
{
name();
height();
return 0;
}
Return a string instead of void.
string name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
return name1;
}
Same thing with height(), for example, that should return an int. Also to get the name in your height function you could do.
int height(string name1)
{
// cout stuff about name
return userHeight;
}
Then you can call it like this:
int main()
{
string userName = name(); // takes the return from name and assigns to userName
int userHeight = height(userName); // passes that string into height()
return 0;
}
More examples of using functions and returning things:
int add(int a, int b)
{
int total = a + b; // the variable total only exists in here
return total;
}
int add4Numbers(int w, int x, int y, int z)
{
int firstTwo = add(w, x); // I am caling the add function
int secondTwo = add(y,z); // Calling it again, with different inputs
int allFour = add(firstTwo, secondTwo); // Calling it with new inputs
return allFour;
} // As soon as I leave this function, firstTwo, secondTwo, and allFour no longer exist
// but the answer allFour will be returned to whoever calls this function
int main()
{
int userA = 1;
int userB = 7;
int userC = 3;
int userD = 2;
int answer = add4Numbers( userA, userB, userC, userD ) // this grabs the value from allFour from inside the add4Numbers function and assigns it to my new variable answer
return answer; // now equals 13
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem in the for-construction, I don't know why my counting starts from 0 but you can't write on it(you can write starting with Register 1).I've marked in the code where is the problem.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string day, date;
int registerCount;
std::cout << "INPUT DATA"
<< std::endl << std::endl
<< "Enter the day in which you want to perform the register: "
<< std::endl;
std::cin >> day;
std::cout << "DATE:" << std::endl;
std::cin >> date;
std::cout << "Enter the number of registers you wanna perfom for the day "
<< day << ":" << std::endl;
std::cin >> registerCount;
std::vector<std::string> registers(registerCount);
for (int i = 0; i < registerCount; ++i) **here is the problem**
{
std::cout << "Register " << i << ":" << std::endl; **it starts from 0 but you can't write on Register 0**
std::getline(std::cin, registers[i]);
}
std::cout << "The data for the day of " << day << " are the following: "
<< std::endl;
std::cout << "DATE: " << date << std::endl;
for (int i = 0; i < registerCount; ++i)
std::cout << registers[i] << std::endl;
}
Use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Header - #include<limits>
after your
std::cin >> registerCount;
to "eat up" the trailing newline coming from previous std::cin which is cause of skipping of the first input
for (int i = 0; i < registerCount; ++i)
You're defining i as starting at 0 here. Very simply, all you need to do is change the "i = 0" to "i = 1".
The way you're assigning to that vector looks a little bit wrong too. I think you need to use push_back() on the vector in that syntax, but I'm not too familiar with all the different operators for vector.