GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2006, 00:29
daking_09 daking_09 is offline
New Member
 
Join Date: Mar 2006
Posts: 15
daking_09 is on a distinguished road

Stuck


Sorry just stuck on this quesiton. this is the question and below is what ive done so far, dont know how to do a calculation that would add the purposed values.

For this question create two files called A2Part3.txt and A2Part3.cpp.
Write a program that asks the user to enter a positive integer value.
The program should use an iterative control structure (loop) to get the sum of all integers
from 1 up to and including the number entered, excluding:
- All numbers that are wholly divisible by 3.
- All even numbers that are less than or equal to 10.
For example, if the user enters 50, the program should compute 1 + 5 + 7 + 11 + 13 + 14 +
16 + … + 50.
If the input provided by the user is a negative number or 0, then your program merely
displays an appropriate error message. The following shows 2 sample interactions with the
program.


CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    int num;
	
	cout << "Enter a positive integers: ";	
	cin >> num;
		
	if (num <= 0) 
	{		
		cout << "\nError: Entered Integer must be greater than zero\n";	 
	}
	
	
		
    
	
	return 0;
}
  #2  
Old 03-Apr-2006, 00:49
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Stuck


so once you know num, you can create a for loop that will count up from one to num. keep a variable (defined outside the loop) that will hold your total. each time through the loop, check whether the counter is divisible by three (use the modulus operator) or whether it is even and <= 10. Otherwise, add it to the total.
CPP / C++ / C Code:
if (i % 3 == 0) continue; //if divisible by three, no good
if ( (i <= 10) && (i % 2 == 0) ) continue; //if less than or equal to 10 and even
total += i;
  #3  
Old 03-Apr-2006, 01:04
daking_09 daking_09 is offline
New Member
 
Join Date: Mar 2006
Posts: 15
daking_09 is on a distinguished road

Re: Stuck


This is what i got but dont work, it makes sense what your saying to me but trying to put that into hard code, is where im strugging.

CPP / C++ / C Code:
#include <iostream>
//mm287@uow.edu.au
using namespace std;

int main()
{
    int num, total;
	
	cout << "Enter a positive integers: ";	
	cin >> num;
		
	if (num <= 0) 
	{		
		cout << "\nError: Entered Integer must be greater than zero\n";	 
	}
	
	 count = 0;
	 while count <= num
	 total = 0
	 total = num + 1
	 if (num % 3 == 0) continue; //if divisible by three, no good
   	 if ( (num <= 10) && (num % 2 == 0) ) continue; //if less than or equal to 10 and even 
	 total += num;
	
	return 0;
}
  #4  
Old 03-Apr-2006, 01:26
daking_09 daking_09 is offline
New Member
 
Join Date: Mar 2006
Posts: 15
daking_09 is on a distinguished road

Re: Stuck


Got this much now.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    int num = 0,totnum = 0, total = 0;
	
	cout << "Enter a positive integers: ";	
	cin >> num;
		
	while (num <= 0) 
	{		
		cout << "\nError! You entered an incorrect value.\n";
		cout << "Enter a positive integers: ";	
		cin >> num;	 
	}
	
	while(totnum <= num)
	{	
		if (totnum % 3 == 0) continue; 
		if ( (totnum <= 10) && (totnum % 2 == 0) ) continue;
		
		
		
			totnum++;
		else
		{
			total = total + totnum;
			totnum++;
		}
	}
	cout << "Start" << "End" << "Sum" << endl; 
	cout << " 1 " << "   " << num  << "   " << total << endl;
	return 0;

}
  #5  
Old 03-Apr-2006, 10:53
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Stuck


Quote:
Originally Posted by daking_09
Got this much now.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    int num = 0,totnum = 0, total = 0;
	
	cout << "Enter a positive integers: ";	
	cin >> num;
		
	while (num <= 0) 
	{		
		cout << "\nError! You entered an incorrect value.\n";
		cout << "Enter a positive integers: ";	
		cin >> num;	 
	}
	
	while(totnum <= num)
	{	
		if (totnum % 3 == 0) continue; 
		if ( (totnum <= 10) && (totnum % 2 == 0) ) continue;
		
		
		
			totnum++;
		else
		{
			total = total + totnum;
			totnum++;
		}
	}
	cout << "Start" << "End" << "Sum" << endl; 
	cout << " 1 " << "   " << num  << "   " << total << endl;
	return 0;

}
Did you read the Guidelines -- esp the one about error messages (#2)? That will help both us and you figure out what is wrong.

You almost have it. What if does that else refer to?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing or corrupt ntfs.sys crassbones Computer Software Forum - Windows 27 13-Nov-2007 13:48
stuck on Structs eleet C Programming Language 14 14-Apr-2005 15:42
Define Functions Nexa C Programming Language 6 16-Nov-2004 10:58
RSA program fwongmc C Programming Language 11 08-Nov-2004 23:15
C++ sums and average homz C++ Forum 35 16-May-2004 17:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:01.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.