Why is my code printing every number? [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 6 years ago.
Improve this question
I have written this code that reads 10 integers from the user, stores them in an array, then calculates and prints the average. It is also supposed to print the numbers in the array that are greater than or equal to the average. Instead, my code prints out every number in the array. How can I fix this?
Also, if anyone has any tips on how to simplify this code, that would be much appreciated, as well.
#include <iostream>
using namespace std;
int main()
{
const int ENTER_NUM = 10;
int integer[ENTER_NUM];
cout << "Enter "<<ENTER_NUM<<" numbers: "<<endl;
cin >> integer[0];
cin >> integer[1];
cin >> integer[2];
cin >> integer[3];
cin >> integer[4];
cin >> integer[5];
cin >> integer[6];
cin >> integer[7];
cin >> integer[8];
cin >> integer[9];
int sum;
sum = integer[0]+integer[1]+integer[2]+integer[3]+integer[4]+integer[5]+integer[6]+integer[7]+integer[8]+integer[9];
int average;
average = sum/ENTER_NUM;
cout<<"Average is: "<<average<<endl;
for(int i=0; i<10; i++)
{
if (integer[i]>=average);
cout<<"Number in array greater than or equal to the average: "<<integer[i]<<endl;
}
return 0;
}

You have a spurious ; after the if in the loop.
When you always use {} on ifs and similar control structures instead of relying on the "one line exception", this error is less likely to happen.

if (integer[i]>=average); contains a semicolon at the end, removing its ability to control the print statement after it. Turn up the warning level on your compiler and you'd have a message about this

Related

How to correctly implement a while loop that reads a sequence of inputs? [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 5 months ago.
Improve this question
Write a while loop that sums all integers read from input until a negative integer is read. The negative integer should not be included in the sum.
Ex: If input is 20, 45, 34, 5, -44, then the output is:
104
#include <iostream>
using namespace std;
int main() {
int numInput;
int numInts;
numInts = 0;
cin >> numInput;
while (numInput >= 0) {
cout << numInts << endl;
numInts = numInts + numInput;
}
cout << numInts << endl;
return 0;
}
The problem is that currently the condition inside the while loop doesn't depend on anything that you do inside the loop.
To solve this you can move the cin >> numInput to inside the condition as shown below:
int main() {
int numInput;
int sum = 0;
//take input and check it
while (cin >> numInput && numInput >= 0) {
sum += numInput;
std::cout <<"current sum is: "<<sum <<std::endl;
}
cout << sum << endl;
return 0;
}
Demo
The loop terminates when numInput is less than 0. In your code you read only one number. So if that number is greater than zero then you get an infinite loop because you never read a second number.
Your code should look something like this
cin >> numInput; // read the first number
while (numInput >= 0) {
cout << numInts << endl;
numInts = numInts + numInput;
cin >> numInput; // read the next number
}
See how the above code reads a new number each time around the while loop. This way when a negative number is entered the loop will terminate.

‘i’ was not declared in this scope for (i = 0; i <= years; i++) [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
For some reason, myprogramminglab keeps saying that I have not declared what 'i' and I don't understand why.
#include <iostream>
using namespace std;
int main()
{
int years;
double cost, inflaRate;
cout<<"Enter the current price of pencils:";
cin >> cost;
cout<<"Enter the number of years in the future that
you will buy the pencil:";
cin >> years;
cout<<"Enter the inflation rate as a percentage." <<
endl;
cin >> inflaRate;
inflaRate /= 1;
cout << "The price of pencils will be " << cost;
for (i = 0; i <= years; i++) //Keeps telling me I have not declared 'i' here
{
cost += (cost*inflaRate);
}
cout << cost << "in" << years << "years." << endl;
system("pause");
return 0;
}
For some reason ...
That reason is because you haven't actually declared i. You can fix that with a simple change to your for loop:
for (int i = 0; i <= years; i++) // No longer complains you have not declared 'i' :-)
// ^^^
// Declare it!

C++: How to get two separate number inputs from text file, with dependency [duplicate]

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
this my code but i get infinite loop with the first number
I want to read the integers fromt he file and store them in the array
the file contains:
8 5 12 1 2 7
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int n = 0; //n is the number of the integers in the file ==> 12
int num;
int arr[100];
ifstream File;
File.open("integers.txt");
while(!File.eof())
{
File >> arr[n];
n++;
}
File.close();
for(int i=0;i<12;n++)
{
cout << arr[i] << " ";
}
cout << "done\n";
return 0;
}
Any help please
I agree with #ravi but I some notes for you:
If you don't know how many integers are in the file and the file contains only integers, you can do this:
std::vector<int>numbers;
int number;
while(InFile >> number)
numbers.push_back(number);
You need to #include<vector> for this.
it would be better if you read how many integers are in the file and then use loop to read them:
int count;
InFile >> count;
int numbers[count]; //allowed since C++11
for(int a = 0; a < count; a++)
InFile >> numbers[a];
Note: I didn't check for successful read, but it is a good practice to do so.
Your loop should be:-
for(int i=0; i < n ; i++)
{
cout << arr[i] << " ";
}

I'm Receiving 'Invalid Type Int[Int]...', But It's Making No Sense [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 7 years ago.
Improve this question
I need to complete a program where your array hold ten test scores. Then, you pass that array through a function that'll display the highest score of the ten.
#include <iostream>
using namespace std;
void showValue(int);//Function prototype
int main()
{
const int results = 10;
double tscores[results];
int result;
//Get the ten test scores from the user.
cout << "Please enter the ten test scores." << endl;
cin >> tscores[0];
cin >> tscores[1];
cin >> tscores[2];
cin >> tscores[3];
cin >> tscores[4];
cin >> tscores[5];
cin >> tscores[6];
cin >> tscores[7];
cin >> tscores[8];
cin >> tscores[9];
//Now, the program should pass the arry through a
//function to find the highest test score.
int max = 0;
for (int i = 0; i < results; i++)
{
if (result[0] > max)
{
max = result[0];
}
}
cout << "The highest score is " << max << endl;
return 0;
}
These are the errors I got:
Prog4r.cpp: In function ‘int main()’:
Prog4r.cpp:37:16: error: invalid types ‘int[int]’ for array subscript
Prog4r.cpp:39:19: error: invalid types ‘int[int]’ for array subscript
I just can't seem to fix the issues. Any help would be appreciated.
In the code below, you try and access an index from the variable named result, which is not an array.
if( result[0] > max )
{
max = result[0];
}
A few things are wrong here,
You want to access the array variable named tscores.
You want to use the loop variable instead of 0.
Your variable max should be the same type as tscores.
double max = 0.0;
for( int i = 0; i < results; ++i )
{
if ( tscores[i] > max )
{
max = tscores[i];
}
}

Reading integers from file and store them in array C++ [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
this my code but i get infinite loop with the first number
I want to read the integers fromt he file and store them in the array
the file contains:
8 5 12 1 2 7
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int n = 0; //n is the number of the integers in the file ==> 12
int num;
int arr[100];
ifstream File;
File.open("integers.txt");
while(!File.eof())
{
File >> arr[n];
n++;
}
File.close();
for(int i=0;i<12;n++)
{
cout << arr[i] << " ";
}
cout << "done\n";
return 0;
}
Any help please
I agree with #ravi but I some notes for you:
If you don't know how many integers are in the file and the file contains only integers, you can do this:
std::vector<int>numbers;
int number;
while(InFile >> number)
numbers.push_back(number);
You need to #include<vector> for this.
it would be better if you read how many integers are in the file and then use loop to read them:
int count;
InFile >> count;
int numbers[count]; //allowed since C++11
for(int a = 0; a < count; a++)
InFile >> numbers[a];
Note: I didn't check for successful read, but it is a good practice to do so.
Your loop should be:-
for(int i=0; i < n ; i++)
{
cout << arr[i] << " ";
}