MedVision ad

Help with c++!! Urgent! (1 Viewer)

MuffinMan

Juno 15/4/08 :)
Joined
Nov 6, 2004
Messages
3,975
Location
Liverpool, NSW
Gender
Male
HSC
2005
I have no idea why this program hangs when i type a positive integer greater than 1

The idea of the code is to add all the numbers from 1 to the number the user has inputted except when num mod 3 == 0, num < 10 && num mod 2 == 0;
HELP!!!!


code:

Code:
#include <iostream>
using namespace std;

int main()
{	 
	int sum, endingInteger, startingInteger = 1;
	
	cout <<"Enter an integer" << endl;
	cin >> endingInteger;
	
	while (endingInteger < 1)
		{
		cout <<"Error! you have entered an invalid value" << endl;
		cin >> endingInteger;
		}
	
	while (endingInteger > startingInteger)
		{
		while (endingInteger >= 1)
	
		
		
		{
			while (startingInteger <= 10)
				{
					switch (startingInteger % 2)
					{
					case 0: sum == sum;
							startingInteger++;
							break;
					case 1: sum = sum + startingInteger;
							startingInteger++; 
					}
				
				}
			}	 
					
			
			while (startingInteger > 10)
				if (startingInteger % 3 != 0)
				{
				sum = sum + startingInteger;
				startingInteger++;
				}
				
			else
				{
				sum == sum;
				startingInteger ++;
				}
			}
		
		cout <<" START\t" <<"      END\t" << "SUM" << endl;
		cout << "     1\t" << " \t"  <<  endingInteger << " \t " << sum  <<  endl;
				
	return 0;
}
 
Last edited by a moderator:

MuffinMan

Juno 15/4/08 :)
Joined
Nov 6, 2004
Messages
3,975
Location
Liverpool, NSW
Gender
Male
HSC
2005
Damn vbulletin wouldnt accept indentation
Heres it in .cpp
open with wordpad
 
Last edited:

MuffinMan

Juno 15/4/08 :)
Joined
Nov 6, 2004
Messages
3,975
Location
Liverpool, NSW
Gender
Male
HSC
2005
to add all numbers from 1 to the number inputted, except when num < 10 and num mod 2 == 0 and except when num mod 3 == 0
 

AntiHyper

Revered Member
Joined
Sep 16, 2004
Messages
1,103
Location
Tichondrius
Gender
Male
HSC
2005
Why don't you just use scanf("%i", &<var_name>);
That way the compiler would know definitely what type of variable would be used as the input. Rather than cin.

Other than that, a quick trace led me to believe that the while loop on line 19 would always be infinitely true ie. endingInteger>=1 at all times, maybe your cpu would be able to detect this infinite loop and close down the program.
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
MuffinMan said:
to add all numbers from 1 to the number inputted, except when num < 10 and num mod 2 == 0 and except when num mod 3 == 0
Code:
read endnumber
validate endnumber, use scanf as suggested
sum = 0
for num from startnumber to endnumber
  if num < 10 or num mod 2 == 0 or num mod 3 == 0 then
    do nothing
  else
    sum += num
  end if
end for
print sum
This should do the job according to your description, not sure why you need these nested loops.
 
Last edited:

Lazarus

Retired
Joined
Jul 6, 2002
Messages
5,965
Location
CBD
Gender
Male
HSC
2001
Just so you know, I think the problem was this line:

Code:
		while (endingInteger >= 1)
'endingInteger' never changes, so if it enters the loop, it's always going to be greater than or equal to 1.

You do have a 'break' statement at one point, but it's nested in yet another loop, so it will only break out of the inner loop and not the outer loop.
 
Joined
Feb 18, 2005
Messages
79
Location
New College - UNSW
Gender
Male
HSC
2006
Instead of using the loops why don't you add it up using the sum of a series formula

3 + 7 + 11 + 15 + ··· + 99 has a1 = 3 and d = 4. To find n, use the explicit formula for an arithmetic sequence.

We solve 3 + (n – 1)·4 = 99 to get n = 25.

Sn = 25/2[2 X 3 + (25 -1) X 4] = 1275
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top