Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Im trying to create a program which displays the minimum and maximum integers as prompted but the user. The maximum is always correct, but the minimum always comes out as 0. Don't know what Im missing from my code?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int intsWanted, min, max, input;
do {
cout << "How many integers would you like to
enter?" << endl;
cin >> intsWanted;
} while (intsWanted < 1);
cout << "Please enter " << intsWanted << " integers." << endl;
cin >> min;
min = max;
intsWanted--;
while (intsWanted >= 1) {
cin >> input;
if (input > max) max = input;
if (input < min) min = input;
intsWanted--; }
cout << "min: " << min << endl;
cout << "max: " << max << endl;
return 0;
}
Change your line min=max to max=min and that will solve your question !
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've tested my array separately to make sure it is working and holding all the values and it is but for some reason when I run it through my for loop it just prints out 10.95 in 3 rows and 3 columns I don't understand why it isn't pull the rest of the values from my table.
Here is the assignment:
Write, compile, and run a C++ program to input the following values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59 After the data has been entered, have your program display the values in 3 rows and 3 columns.
Here is the code I have written:
#include <iostream>
#include <iomanip>
using namespace std;
const int COLS = 3;
const int ROWS = 3;
int main()
{
const int num_items = 9;
float prices[num_items];
cout << "Enter the prices of your " << num_items << " items: ";
cin >> prices[0];
cin >> prices[1];
cin >> prices[2];
cin >> prices[3];
cin >> prices[4];
cin >> prices[5];
cin >> prices[6];
cin >> prices[7];
cin >> prices[8];
float table[ROWS][COLS] = {{prices[0], prices[1], prices[2]},
{prices[3], prices[4], prices[5]},
{prices[6], prices[7], prices[8]}};
cout << "The prices you have entered are:\n";
for (int x = 0; x < ROWS; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << setw(6) << table[ROWS][COLS] << " ";
}
cout << endl;
}
return0;
}
cout << setw(6) << table[ROWS][COLS] << " ";
should be
cout << setw(6) << table[x][y] << " ";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So the idea is to ask the user for each element of the array, but after an input is given for the first question (where it asks for the amount of elements), nothing happens. Can't figure out why.
#include <iostream>
int main()
{
int numGrades;
tryAgain:
std::cout << "Enter number of grades" << std::endl;
std::cin >> numGrades;
if (numGrades > 30)
{
std::cout << "Please enter a valid number of grades" << std::endl;
goto tryAgain;
}
int grades[numGrades - 1];
int gradeCount = 0;
while (gradeCount < numGrades);
{
std::cout << "Enter grade number" << gradeCount + 1 << ":";
std::cin >> grades[gradeCount];
++ gradeCount;
}
std::cout << grades;
return 0;
}
The constuction while (true); means while (true) {} (i.e. infinite loop).
So, when you write
while (gradeCount < numGrades);
{
// ...
}
you have the following:
while (gradeCount < numGrades)
{
}
{
// ...
}
Second block will never be executed if gradeCount < numGrades.
You are using
while (gradeCount < numGrades);
with a semi-colon (;) at the end of this line so the next line will not exectue because the condition is always true as there is no increment or decrement in the respective variables.
In short just remove the (;)
while (gradeCount < numGrades)
Please see this code, there were few problems. One is semicolon on while loop & another one is printing grades & memory allocation of the grades. Memory static allocation must need a constant value. Here a dynamic allocation is added as the grades number is not fixed or constant... Here is the code:
#include <iostream>
int main()
{
int numGrades;
tryAgain:
std::cout << "Enter number of grades" << std::endl;
std::cin >> numGrades;
if (numGrades > 30)
{
std::cout << "Please enter a valid number of grades" << std::endl;
goto tryAgain;
}
int *grades = (int *)malloc(numGrades * sizeof(int)); //allocating dynamic memory
int gradeCount = 0;
while (gradeCount < numGrades)
{
std::cout << "Enter grade number" << gradeCount + 1 << ":";
std::cin >> grades[gradeCount];
++ gradeCount;
}
for(int i =0;i<numGrades;i++)
{
std::cout << grades[i] << std::endl;
}
free(grades);//releasing memory
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my while loop wont run on the conditions I set it.
The purpose of the program is to determine the amount of years it will take a deposited amount to mature using the deposit amount, interest rate and target amount.
The program just stops after the target amount has been entered, unless I change the while statement from <= to >= in which case it runs the loop but returns the number of years set at round 100's or 1000 etc...
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
Thanks in advance!
The problem is an unfortunate suffix:
while (balance <= target);
// ^
That is equivalently:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
Just drop the semicolon.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I can't seem to be able to get this to work properly, I keep getting "Stack around the variable 'judge' was corrupted". Where did I go wrong? It seems to run properly, but at the end I get the error after I close and my lowNum value won't seem to process correctly.
#include <iostream>
using namespace std;
const int SIZE = 5;
double getJudgeData(int judge);
double findLowest(double num[SIZE]);
double findHighest(double num[SIZE]);
int main()
{
double
judge[SIZE],
highNum,
lowNum;
for(int num = 1; num <= SIZE; num++)
{
judge[num] = getJudgeData(num);
highNum = findHighest(judge);
lowNum = findLowest(judge);
}
cout << highNum << endl;
cout << lowNum << endl;
system ("pause");
return 0;
}
double getJudgeData(int judge)
{
double score;
cout << "What is the score given by judge " << judge << "?" << endl;
cin >> score;
while(score < 0 || score > 10)
{
cout << "Please enter a score between 0 and 10.\n\n";
cout << "What is the score given by judge " << judge << "?" << endl;
cin >> score;
}
return score;
}
double findHighest(double num[SIZE])
{
double high = 0;
for(int count=0;count<SIZE;count++)
{
if(num[count]>high)
high=num[count];
}
return high;
}
double findLowest(double num[SIZE])
{
double low = 10;
for(int count=0;count<SIZE;count++)
{
if(num[count]<low)
low=num[count];
}
return low;
}
C++ used arrays from 0 to n-1. Your code is using 1 to n, so you "run off the end" of the array judge (and corrupt the variable around it).
Suggest you close the question since it's just a typo.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hey guys I've got this piece of code, posted below. I've added the while loop to make sure that only numeric input is used, but when I use it, it requires me to input the number twice, or press enter and then input the number.
Output would be:
Input number : 1
1
then it would it would print the results. How can I fix this Cheers.
void Dictionary::SearchNumeric()
{
int x;
cout << "Input number : ";
while (!(cin >> x))
{
cout << "Invalid input. Try again: ";
cin.ignore(numeric_limits<streamsize>::max());
}
string searchWord = myWords[x]->word;
cout << "Word searched: " << searchWord << endl;
cout << "Definition: \n" << myWords[x]->definition << endl;
cout << "Type: " << myWords[x]->type << endl;
int wordIndex = 0;
//while (myWords[wordIndex]->word.compare(x) != 0) {
//needs to return scrabble score
wordIndex++;
//break;
//}
}
Get rid of the first cin >> x;, set the string searchWord after the while