your program should read the first number -- 5 in your example -- then have a loop that reads that many more numbers. If the file contains more than one line like that, the program should have an outer loop that performs those actions for each line in the file.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Take a look at function fscanf() that delimits on whitespaces. Nothing wrong with using it in C++, it is part of ANSI C++.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Take a look at function fscanf() that delimits on whitespaces. Nothing wrong with using it in C++, it is part of ANSI C++.
Nothing wrong with it but totally unnecessary. cin is just C++'s version of scanf() . And there is no reason to read from a file using fscanf() .
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I got it!!! now how in the hell do I add increments? I'm stuck on this... I know this looks busy but this was thru trial and error tonight... I'm so proud of me!!!:mrgreen:
{
int a = 0;
int b =0;
int sum =0;
int counter =0;
int number =a;
{
cout << "how many numbers you want added? " << endl;
cin >> a;
cout << "number to increment? " << endl;
cin >> b;
for ( int counter = b; counter <= a * b; counter+= b )
cout << counter << " ";
<strong> sum = b * ;</strong>
cout << "Sum is: " << sum << endl;
}
you have completly missed the point of the assigment. You must get the numbers from a data file, not from the keyboard. You have to use an ifstream object to read the numbers from a file.
As for getting the sum -- just upgrade the sum after reading each value;
int number;
int nitems;
ifstream infile("file.txt");
// find number of items to read
infile >> nitems;
loop starts here
// read a number
infile >> number;
sum += number;
end of loop
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I'm sure you know what files are -- If you are using MS-Windows operating system and use Windows Explorer you will see hundreds of files and folders on your computer.
Normally when programmers use the terms "read a number" it means to read it from a data file. But I may have misunderstood your requirements. If that is the case, then just use cin to read the values from the keyboard
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number";
cin >> number;
// now you must flush the keyboard of the <Enter> key
cin.ignore();
return 0;
}
>>I definitely wouldn't understand how to get the sum by upgrading it after reading each value
I already posted the example -- the linesum += number;
. The += operator means to add number to the previous value of sum. Just a shorthand way of saying this:
sum = sum + number;
from what you posted previously, you need to put the prompt for a number INSIDE the for loop.
cout << "how many numbers you want added? " << endl;
cin >> a;
cin.ignore(); // flush keyboard of <Enter> key
// on this for loop, we want to increment the variable <strong>counter</strong>
// until it reaches the value of variable <strong>a</strong>.
for ( int counter = 0; counter < a; counter++)
{
cout << "enter a number: " << endl;
cin >> b;
cin.ignore(); // flush keyboard of <Enter> key
// keep a running total of the numbers entered.
sum = sum + b;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343