| | |
I need help quick!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
Ok, I'm writing a simple for loop with an accumulator for my class tonight and can't figure out what I'm doing wrong. I want the program to accept the first 10 even numbers from the keyboard and add them up and display an error message if someone enters an odd number, heres what I've got so far and the only real problem I'm having is getting the numbers to add up correctly. If some one could give me a quick response it would be great... Thanks so much.
// This program will accept and sum the first 10 even numbers as read from the keyboard.
#include <iostream>
using namespace std;
//constants
const int terminatingvalue= 10;const int two=2;const int zero=0;
int main ()
{
//Variables
int ctr=1;int num;int sum=0;
// procces
for (int ctr=1; ctr<=10; ctr++)
{
cout<<"Enter a number"<<endl;
cin>>num;
if (num%two==zero)
{
sum=sum+num;
ctr++;
}
else
cout<<"Please enter an even number"<<endl;
cout<<"Enter a number"<<endl;
cin>>num;
}
cout<<"Total is:"<<sum<<endl;
//output
return 0;
}
// This program will accept and sum the first 10 even numbers as read from the keyboard.
#include <iostream>
using namespace std;
//constants
const int terminatingvalue= 10;const int two=2;const int zero=0;
int main ()
{
//Variables
int ctr=1;int num;int sum=0;
// procces
for (int ctr=1; ctr<=10; ctr++)
{
cout<<"Enter a number"<<endl;
cin>>num;
if (num%two==zero)
{
sum=sum+num;
ctr++;
}
else
cout<<"Please enter an even number"<<endl;
cout<<"Enter a number"<<endl;
cin>>num;
}
cout<<"Total is:"<<sum<<endl;
//output
return 0;
}
Last edited by thinfineline; Oct 23rd, 2008 at 5:55 pm.
You're doing input of num twice in the loop, but only adding it once.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
If you leave it as is, it will only perform 10 repetitions of the loop, including invalid inputs. Then you'd only get the first 10 - (number of errors) even integers in your summation. My suggestion is this:
Let me explain the for structure I used here.
for (int ctr=1; ctr<=10; )
- your original for loop incremented ctr on each run (int ctr=1; ctr<=10; ctr++)
- what I'm doing different is removing the incrementing of ctr from every run, so that
invalid numbers that are input (odds) do not count toward the number of repetitions
- this way you get 10 total numbers and not a lower number
C++ Syntax (Toggle Plain Text)
for (int ctr=1; ctr<=10; ) { cout<<"Enter a number"<<endl; cin>>num; if (num%two==zero) { sum=sum+num; ctr++; // only increment when a valid summation occurs } else cout<<"Please enter an even number"<<endl; cout<<"Enter a number"<<endl; cin>>num; }
Let me explain the for structure I used here.
for (int ctr=1; ctr<=10; )
- your original for loop incremented ctr on each run (int ctr=1; ctr<=10; ctr++)
- what I'm doing different is removing the incrementing of ctr from every run, so that
invalid numbers that are input (odds) do not count toward the number of repetitions
- this way you get 10 total numbers and not a lower number
Last edited by chococrack; Oct 23rd, 2008 at 7:36 pm.
I would love to change the world, but they won't give me the source code
![]() |
Similar Threads
- How can i get Quick Launch on start Bar in Windows-NT(workstation) (Windows NT / 2000 / XP)
- Quick Launch toolbar lost (Windows 95 / 98 / Me)
- Display Your Quick Launch Toolbar (Windows tips 'n' tweaks)
- Display Your Quick Launch Toolbar (Windows 95 / 98 / Me)
- Display the Quick Launch Toolbar (Windows tips 'n' tweaks)
- Quick Search Tips (Windows tips 'n' tweaks)
- motherboard specs (Motherboards, CPUs and RAM)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: C++ calender
- Next Thread: Reading Specific Ints and Chars from a String
Views: 443 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






