954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Homework again...just want help reading...for now...

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.

chicagoavonlady
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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;
 
 
}
chicagoavonlady
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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!

chicagoavonlady
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 
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
Moderator
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

chicagoavonlady
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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!!!!

chicagoavonlady
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You