for loop not incrementing through table of array C++ [closed] - c++

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] << " ";

Related

2 Error ; No Operator ">>" and expression must have constant value [closed]

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 2 months ago.
Improve this question
I try to create a program for football match with array in cpp
#include <iostream>
#include <string>
using namespace std;
// Struct untuk tim
struct Tim {
string nama_tim;
int skor;
};
// Fungsi untuk menampilkan hasil pertandingan
void tampilkan_hasil(Tim tim[], int jumlah_tim) {
cout << "Hasil pertandingan: " << endl;
for (int i = 0; i < jumlah_tim; i++) {
cout << tim[i].nama_tim << ": " << tim[i].skor << " gol" << endl;
}
}
// Fungsi untuk menentukan pemenang
void tentukan_pemenang(Tim tim[], int jumlah_tim) {
int index_tim_pemenang = 0;
for (int i = 0; i < jumlah_tim; i++) {
if (tim[i].skor > tim[index_tim_pemenang].skor) {
index_tim_pemenang = i;
}
}
cout << "Pemenangnya adalah " << tim[index_tim_pemenang].nama_tim << endl;
}
int main() {
// Masukkan jumlah tim
static const int jumlah_tim;
cout << "Masukkan jumlah tim: ";
cin >> jumlah_tim;
// Buat array tim
Tim tim[jumlah_tim];
// Masukkan data tim
for (int i = 0; i < jumlah_tim; i++) {
cout << "Masukkan nama tim ke-" << i + 1 << ": ";
cin >> tim[i].nama_tim;
cout << "Masukkan skor tim ke-" << i + 1 << ": ";
cin >> tim[i].skor;
}
// Tampilkan hasil pertandingan
tampilkan_hasil(tim, jumlah_tim);
// Tentukan pemenang
tentukan_pemenang(tim, jumlah_tim);
return 0;
}
The First Error : "no operator ">>" matches these operands " [LN 35]
cin >> jumlah_tim;
Second Error : "expression must have a constant value" [LN 38]
Tim tim[jumlah_tim];
it should contain the user input to array data
Two errors
If a variable is constant then (obviously) you cannot change it. And if a variable cannot be changed then you cannot read into that variable using >>.
Array sizes must be compile time constants in C++. Since the size of an array must be known to the compiler, you cannot read the size of an array from cin.
The solution to your problems is to use a vector. Vector sizes do not have to be constants in C++.
#include <vector>
// Masukkan jumlah tim
int jumlah_tim;
cout << "Masukkan jumlah tim: ";
cin >> jumlah_tim;
// Buat vector tim
vector<Tim> tim(jumlah_tim);
Obviously once you change tim to be a vector further changes are needed to the rest of your code. For example
void tampilkan_hasil(const vector<Tim>& tim) {
cout << "Hasil pertandingan: " << endl;
for (size_t i = 0; i < tim.size(); i++) {
cout << tim[i].nama_tim << ": " << tim[i].skor << " gol" << endl;
}
}

Minimum output is always coming out as 0 [closed]

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 !

CODE is not executing in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
TASK 2 remains un-executed. Task 1 works fine, I input the yield values of the cows; but then the code stops running. A warning says that Herdtotalweek may be uninitialized. But I don't know how to fix that. There are no other warnings or errors.
#include <iostream>
#include <string>
using namespace std;
int main() { //Task 1
int Herdsize;
int Day;
float MilkYield1;
float MilkYield2;
int count;
cout << "Please input herd size" << endl;
cin >> Herdsize;
while (Herdsize < 1 || Herdsize > 900) {
cout << "Please re-input herdsize between 1 and 900" << endl;
cin >> Herdsize;
}
int CowID[Herdsize + 1];
float DailyYield[Herdsize * 7];
float WeeklyYieldpercow[Herdsize * 14];
for (count = 1; count < Herdsize + 1; count++) {
cout << "Input 3 digit cow id ";
cin >> CowID[count];
while (CowID[count] < 1 || CowID[count] > 999) {
cout << "Please re-input cow a 3 digit cow id " << endl;
cin >> CowID[count];
}
for (Day = 1; Day < 8; Day++) {
cout << "Please input first milk yield of cow,day";
cout << Day;
cout << endl;
cin >> MilkYield1;
cout << "Please input second milk yield day:";
cout << Day;
cout << ", if there is a second yield if not enter 0";
cout << endl;
cin >> MilkYield2;
}
DailyYield[((count - 1) * 7) + Day] = MilkYield1 + MilkYield2;
WeeklyYieldpercow[count] = WeeklyYieldpercow[count] +
DailyYield[((count - 1) * 7) + Day];
}
// TASK 2
int count2 = 1;
float Herdtotalweek;
float Averagevolume;
for (count = 1; count2 < Herdsize + 1; count++) {
Herdtotalweek = Herdtotalweek + WeeklyYieldpercow[count];
}
Averagevolume = Herdtotalweek / Herdsize;
int Herdtotalweekwhole = int(Herdtotalweek + 0.5);
int Averagevolumewhole = int(Averagevolume + 0.5);
cout << "Total weekly volume=";
cout << Herdtotalweekwhole;
cout << "Average volume =";
cout << Averagevolumewhole;
}
instead of float Herdtotalweek; try using float Herdtotalweek = 0; ?
also, in your second for statement, instead of for (count=1;count2<Herdsize+1;count++) try for (count=1;count<Herdsize+1;count++) (you were using count2 instead of count, which was probably a copy/paste error)
The for loop after task2 never completes. It is an infinite loop as you are not updating count2.

C++ while loop not starting [closed]

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;
}

Error when accessing array - stack around the variable 'scores' was corrupted [closed]

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 am new to C++ and have written some code in which I'm getting the following error:
Run-Time Check Failure #2 - Stack around the variable 'scores' was corrupted
What is causing this error?
Here is my code:
#include <iostream> // Enables cout and endl
#include <string>
#include <sstream>
#include "stdafx.h"
using namespace std;
int getInput();
int main()
{
int scores[5];
int i;
int j;
int numberOfScores;
for (i = 0; i < 6; i++) // Sets all 5 elements of the array to zero
{
scores[i] = 0;
}
cout << "How many scores do you have to enter?\n" << endl;
cin >> numberOfScores;
for (j = 0; j < numberOfScores; j++) // Gather test scores and increases each array index as that score is entered
{
scores[getInput()] ++;
}
cout << "The number of zeros: " << scores[0] << endl;
cout << "The number of ones: " << scores[1] << endl;
cout << "The number of twos: " << scores[2] << endl;
cout << "The number of threes: " << scores[3] << endl;
cout << "The number of fours: " << scores[4] << endl;
cout << "The number of fives: " << scores[5] << endl;
return 0;
}
int getInput()
{
int enteredScore;
cout << "Enter the test scores one at a time.\n";
cout << "The range of scores is 0 to 5.\n";
cin >> enteredScore;
if (enteredScore >= 0 && enteredScore <= 5)
{
return enteredScore;
}
else
{
cout << "Error! The range of scores is 0 to 5.\n";
cout << "Enter the test scores one at a time.\n";
cin >> enteredScore;
return enteredScore;
}
}
It seems that this declaration:
int scores[5];
Is incorrect. This creates an array with 5 numbers in it, indices from scores[0-4], however, you constantly refer to score[5], the sixth element of the array throughout your program. I recommend changing to
int scores[6];
The problem:
You are accessing your array out of bounds in several places.
Here you loop through 6 elements when you only have 5:
for (i = 0; i < 6; i++) // Loops through 6 elements
{
scores[i] = 0;
}
Here you call getInput() and use the return value as the index:
scores[getInput()] ++;
However, the first half of the function accepts inputs from the user in the range 0 to 5, thus allowing access to 6 elements:
if (enteredScore >= 0 && enteredScore <= 5)
It gets even worse if the user enters a number outside that range, as they are then given a second opportunity to enter a number, only this time there is no validation and any number they enter is accepted:
cin >> enteredScore;
return enteredScore;
Finally, you again attempt to access a 6th element here:
cout << "The number of fives: " << scores[5] << endl;
Solution:
First, you need to do one of two things:
Change the for loop, if statement, and cout statements so that they do not access index 5
or:
Create the array so that it has 6 elements: int scores[6];
Secondly, you need to fix the bug in your getInput() function so that it validates the input properly. You could try this for example:
int getInput()
{
int enteredScore;
cout << "Enter the test scores one at a time.\n";
cout << "The range of scores is 0 to 4.\n";
cin >> enteredScore;
while (enteredScore < 0 || enteredScore > 4)
{
cout << "Error! The range of scores is 0 to 4.\n";
cout << "Enter the test scores one at a time.\n";
cin >> enteredScore;
}
return enteredScore;
}
You have an error in
cout << "The number of fives: " << scores[5] << endl;
Your array is of size 5 but you are accessing the 6th element.
Same with for (i = 0; i < 6; i++) should be i < 5.