Why does my class interval output only one when I enter 10 or more data? I use while loop here. What's missing?
num= min;
while (num <=max){
cout<< "class interval"<< endl;
cout << num <<setw(5)<<" ";
cout<< (num-1)+ interval<< setw(6);
if (num=max)
break;
I want to have the class interval that I enter from class size to show like
50-60
60-70
70-80
But when I run it the only show is
50-60 and the program ended.
Related
I am attempting to develop a vending machine in C++ in which the user can insert as many 50 cent, 20 cent, and 10 cent coins as they would like to then move on to the 'purchase' of a product.
So far, my primitive code runs smoothly; the issue I am currently facing is that I can only 'insert' coins into the vending machine once, even though I think the 'while' condition in the 'do/while' statement is being executed.
Below you can find my code:
`
#include <iostream>
using namespace std;
int main() {
int productid;
string order, finished;
int amount;
cout << "Welcome to the coffee machine! We have a variety of beverages we can offer. \n Here is our selection: \n 1) Coffee- 50 cents \n 2) Tea- 60 cents \n 3) Cappuccino- 80 cents\n";
cout << "Please select your drink of choice by entering its ID: (1, 2, or 3)";
cin >> productid;
if (productid != 1 && productid != 2 && productid != 3){
cout << "That is an invalid entry; please try again. \n";
cin >> productid;
}
cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
cout << "When finished, please input '0'. ";
cin >> amount;
if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
cout << "That is an invalid coin; please insert coins again.\n";
cin >> amount;
}
do {
amount += amount;
}
while (amount != 0);
return 0;
}
`
I was expecting to be able to insert coins until I input '0', but the code terminal says 'Process finished with exit code 0' after I insert coins once, thereby not allowing me to continue to insert coins.
If anyone has any suggestions as to how I could fix this so that the user can continuously insert coins until they input '0' I would greatly appreciate it.
Please feel free to leave any suggestions as to how to proceed as well.
Thank you
Your do-while looks like this:
do {
amount += amount;
}
while (amount != 0);
What does this do? It adds amount to itself until eventually it is 0. This can happen due to the fact that numbers are stored in a finite number of bits in memory and when due to some operation (value assignment in this case) is greater than the maximum value that can be stored for the given type, then the value will overflow its type limits and may get a smaller value than prior to the addition as a result, maybe even 0.
However, this is definitely not what you want.
You want: to read amount repeatedly until it's 0 and add it to a sum
You actually do: read amount exactly once, then add it to itself until it's 0
How to remedy this:
Move the reading inside the code and make sure that the sum will be a different variable:
cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
cout << "When finished, please input '0'. ";
int sum = 0;
do {
cin >> amount;
if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
cout << "That is an invalid coin; please insert coins again.\n";
} else {
sum += amount;
}
}
while (amount != 0);
You need to put the do { ... } while(...); around the entire block you'd like to repeat. Also, you need a separate variable for the sum.
int amount, sum = 0;
// ...
cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
cout << "When finished, please input '0'. ";
do {
cin >> amount;
while (amount != 50 && amount != 20 && amount != 10 && amount != 0){
cout << "That is an invalid coin; please insert coins again.\n";
cin >> amount;
}
sum += amount;
}
while (amount != 0);
I've also changed an if to a while in your code for the case when the user makes multiple mistakes.
To solve these cases yourself, it's recommended that you either use a debugger and step through your code; or add some logging into the code an check what's going on (e.g., what the loop repeats).
the reason why the program does not ask you for another input is because the do-while loop only includes one line: amount += amount;
Also this is the issue why the program terminates, it gets stuck in this loop of adding amount on itself and when the int overflow happens and the amount gets zero value, the program terminates. You can check this behavior by printing out the amount variable in the loop.
amount += amount;
cout << amount << endl;
To fix the issue, you need to move couple of lines inside do block.
do {
cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
cout << "When finished, please input '0'. ";
cin >> amount;
cout << amount << endl;
if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
cout << "That is an invalid coin; please insert coins again.\n";
cin >> amount;
}
amount += amount;
cout << amount << endl;
}
while (amount != 0);
Also, you need one more variable for the sum of coins (amount). This way if you add only one coin, the amount variable will be doubled.
Hope this helps.
I am working on a project for the office today and I’m running into a bit of a stumble today on it. My code is properly debugged but it when ran it ignores all the parameters I set up and becomes smashed all together in a one line executable (if that makes sense). Here’s the prompt and what I have so far:
“A business needs to calculate the bonus points for sales representatives. The bonus points are based on how much each sales representative sold the year.
Write a program to prompt the user to enter the sales amount for a sales representative. Include different functions for each group of Sales representatives.
Display the bonus points as integers.
Please see the table showing the Groups, Sales, and Bonus points:
Groups Sales Bonus points:
A $0 - $100, 000 500 points
$100, 001 - $1,000,000 1, 500 points
B $1,000,001 - $2,000, 000 2,000 points
$2,000,001 - $3,000, 000 2, 500 points
C $3,000,001 - $4,000, 000 3, 000 points
$4,000,001 and over 5, 000 points
Input Validation: Do not accept negative numbers for sales.”
And here’s what I have tried so far:
#include <iostream>
#include <iomanip>
using namespace std;
float salesamounts; //sales made
int username; // Sales rep name
int main()
{
cout << “Please enter your sales made for the year: $”
cin >> salesamounts;
cout << endl;
// exception on program
if (salesamounts <= -1);
{
if (salesamounts <= -1);
cout << “Value cannot be negative. Please input again.”
}
// Group A placement
for (;salesamount <= 100000;)
{
if ((salesamounts <= 100000)
cout << “Congratulations! You earned 500 Bonus Points!”
break;
{
for (; salesamounts > 100001 < 1000000;)
{
if ((salesamounts > 100001 < 1000000));
cout << “Congrats! 10000 Bonus Points have been credited to you!”
break;
}
According to your requirement, you could use if-elseif-else to judge each group of Sales representatives, which is like:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double salesamounts; //sales made
int username; // Sales rep name
cout << "Please enter your sales made for the year : $" << endl;
while (cin >> salesamounts) {
// exception on program
if (salesamounts < 0.0) {
cout << "Value cannot be negative.Please input again." << endl;
}
//judge salesamounts
else if (salesamounts>0.0 && salesamounts <= 100000.0) {
cout << "Congratulations!You earned 500 Bonus Points!" << endl;
}
else if (salesamounts > 100000.0 && salesamounts <= 1000000.0) {
cout << "Congrats!1500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 1000000.0 && salesamounts <= 2000000.0) {
cout << "Congrats!2,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 2000000.0 && salesamounts <= 3000000.0) {
cout << "Congrats!2,500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 3000000.0 && salesamounts <= 4000000.0) {
cout << "Congrats!3,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 4000000.0 ) {
cout << "Congrats!5,000 Bonus Points have been credited to you!" << endl;
}
else {
break;
}
}
}
By the way, you could also use switch statement to do this.
You did the input part almost correctly, but after that you confused the if-else-if part with for loops.
When taking the user input, you can use a while loop to check whether the value is negative or not. This will make the program to prompt the user to enter a value again if the entered value does not match the criteria:
float salesamounts;
cout<<"Enter the sales amounts for the year:"; //the program will prompt the user initially
cin>>salesamounts; //user inputs the value here
while(salesamounts<0) //this line checks if the value is negative, if yes, it'll continue running this block till the value is non-negative
{
cout<<"Negative numbers not allowed. Please enter again: "; //Suppose the value is negative. Then the program will display this line and prompt the user to enter again
cin>>salesamounts; //user enters the value again
}
The next step in your program is to then check the user input and make the program act accordingly. The if-else statement will do that perfectly:
if (salesamount<=100000) //checks the value. If this is true, then then the following line will run. Else, the program will move to the next check.
cout<<"Bonus point is 500"; //displays the output if the above condition is correct
else if (salesamount<=1000000) //second check
cout<<"Bonus point is 1500"; //displays if the second condition is true
.
.
.
else
cout<<"Bonus point is 5000";
and so on.
Just to clarify, a loop is used only when you want a particular block of code to run repeatedly, whereas, to check or compare two values, you use the if-else statement.
Note that I've not written the second check as if (salesamounts>=100001 && salesamounts<=1000000) and also, I gave the last check as only else, not else if.
I've searched this website and many others for this seemingly common crash people experience and I've read that this error means that a line in my code is trying to access deallocated memory.
I'm a beginner in this language and am trying to play around with arrays in order to fully understand them and I can't seem to figure out what my error is.
I will try to make it as easy and clear as possible: My program simply asks for :
How many subjects and -subsequently- grades the user is going to input
The name of the subject followed by its grade for the number previously entered by the user (So if user entered 3 at the first question the program is going to ask three times to input the subject and grade)
My code is the following :
void moyenne()
{
string subjects[0];
double grade[0];
int lim;
cout << "**Program which calculates the average grade of several subjects, each with coefficient 1 **\n";
cout << "How many subjects are you going to enter grades for?\n";
cin >> lim; // User enters the "limit" to which the "for" loop is going to end
cout << "Please enter seperately the subjects of the grades you'd like to enter followed with their respectful grade.\n";
for (int i = 0; i<=lim-1; i++) // "lim-1" because if lim = x, i will go from 0 to x which is x+1 array slots
{
cout << "Enter subject.\n";
cin >> subjects[i];
cout << "Enter grade for " << subjects[i] << ".\n";
cin >> grade[i];
}
for (int j = 0; j<=lim-1; j++)
{
cout << subjects[j] << grade[j];
}
}
The program systematically and precisely crashes at the third request for a grade, whatever limthe user inputs as long as lim > 2. The error is :
And the console is :
**Program which calculates the average grade of several subjects, each with coefficient 1 **
How many subjects are you going to enter grades for?
3
Please enter seperately the subjects of the grades you'd like to enter followed with their respectful grade.
Enter subject.
Maths
Enter grade for Maths.
15
Enter subject.
Physics
Enter grade for Physics.
17
Enter subject.
English
(lldb)
was doing school project here and run into a problem
My program was support to be user guessing a number which is 47 and if they got it wrong they have 5 total of 5 tries. if still didnt get it the program will exit. If they got it there will be a message saying that how many tries it took for then to get the answer.
Therefore, i have to have a identifier for the number of tries it took. But C++ says that i didnt initialized the identifier. Please help
#include <iostream>
using namespace std;
int main()
{
int intSecretNum = 47;
int intGuess;
int GuessNum;
cout<< "Guess the secret number (between 0 and 100 inclusively): ";
cin>> intGuess;
while(intSecretNum != 47, GuessNum<= 5)
{
if( intGuess < intSecretNum)
cout << "Your Number is smaller than the secret number";
else if (intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
GuessNum++;
if(GuessNum>5)
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
cout<< "You have used "<<GuessNum <<" to guess te secret number which is " <<intSecretNum<<".";
cout<<"program terminated."<<endl;
return 0;
}
Please help =D
You forgot to initialize GuessNum:
int GuessNum = 0;
^^^
Additionally, the comma in the while condition does not do what you think it does, you mean && which is "and" in C++.
#include <iostream>
using namespace std;
int main(){
int num=0;
int total=0;
cout<<"Enter many numbers as you like : "<<endl;
while (cin>>num){
if (num==0){
break;
cout<<"The sum is : " ;
total = total + num;
}
}
system("PAUSE");
return 0;
}
When I run this it runs and when I enter zero the program stops but it did not get the sum of the numbers entered by the user. please help me with this problem. thanks. :)
To print a number do:
cout<<"The sum is : " << total << endl;
You will want to place thebreak; statement as the last line of your conditional if(...){...} statement. When the program reaches the breakstatement, it will exit the while loop and proceed to the end of the program. Right now your loop is exited before cout has the new stream written to it and the total is updated.
Second, you want to put the value of total on the output stream:
cout << "The sum is : " << total << endl;
Currently, you are just printing out the string "The sum is :"
Also, system("PAUSE"); is a shell and OS dependent statement, and usually not seen as good coding practice. You may want to look into another way to accomplish this: http://www.dreamincode.net/forums/topic/30581-holding-the-execution-window-open/