Could somebody help me understand what this is asking for? I have tried different c++ codes and I don't think im reading it right...
NOTE: IM NOT ASKING FOR THE ACTUAL CODE...
I want to try and figure this one out 1st....but I don't think the way I'm reading it is making me understand where I have to put what...here it goes...

Write a program that uses a for statement to sum a sequence of intergers. Assume that the 1st interger read specifies the number of values remaining to be entered. Your program should read only one value per input statement. A typical input sequence might be

5 50 100 150 200 250

I can't understand what goes 1st, the chicken or the egg! ok that was a joke...:lol:

I keep getting blank values with what I'm doing...

Thanks guys.

Recommended Answers

All 9 Replies

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.

Take a look at function fscanf() that delimits on whitespaces. Nothing wrong with using it in C++, it is part of ANSI C++.

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 << " ";
 
    [B] sum = b * ;[/B]
cout << "Sum is: " << sum << endl;
 
 
}

you asking somebody NEW to try something from another language???

P L E A S E ! ! !

(read that really slow...) let me learn C++ first...I'll venture out later!

Thanks anyway!

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() .

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 << " ";
 
    [B] sum = b * ;[/B]
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

but what youre reffering to is not covered in the chapter in which the assignment came from...

my apologies but I don't understand what the "data file" is yet...and I definitely wouldn't understand how to get the sum by upgrading it after reading each value....

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 line sum += 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 [b]counter[/b]
// until it reaches the value of variable [b]a[/b].
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;
}

I know what files are, yeah but when youre putting your words in "un lamen's terms" I had no clue...remember, I just started. I don't get a chance to work on this daily because of full time work schedule and another algorithm class...(I cannot believe it is taking me at least 3 sheets of paper to get an answer for 1 stinkin problem!!!)....\
All the same, I thank you for your assistance. what you have givin me is explained as well as the book!

it's now 4am my time and I have to get ready for work. I will work on the solution after work today...

Again, thanks!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.