Reading integers from file and store them in 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 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] << " ";
}

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

Why is my code printing every number? [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
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

Vector not displaying full contents [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 have written a script so that the odd numbers are stored in a vector and then displayed at the end after putting in a negative number.
For some reason the first odd number is only displayed. Here is the code:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int i=0;
int val=0;
int rem;
vector<int>v;
while(val>=0){
cout<<"Please enter an integer: \n";
cin>>val;
rem=val%2;
if(rem==1)
v.push_back(val);
else;
}
for(int i=0; i<v.size(); ++i);
cout<<"Odd Numbers: " <<v[i]<< "\n";
system("pause");
return 0;
}
Iterating through a vector is best done with an iterator.
for(auto it = std::begin(v); it != std::end(v); it++)
std::cout << "Odd number: " << *it << std::endl;
Be careful with your semicolons:
for(int i=0; i<v.size(); ++i);
// ^^^
cout<<"Odd Numbers: " <<v[i]<< "\n";
You are running the loop:
for(int i=0; i<v.size(); ++i) {
;
}
cout<<"Odd Numbers: " <<v[i]<< "\n";