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 25-Jul-2005, 18:43
amazingme001 amazingme001 is offline
New Member
 
Join Date: Jul 2005
Posts: 5
amazingme001 is on a distinguished road
Exclamation

Please help with this C++ program


This program is supposed to get two numbers as an input. Then it should find and print all the odd numbers. b) it should print the sum of all even numbers.
I have been trying for hours, but my while loops dont work as they are supposed to. Anyone can debug??????

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <iomanip>

#include "PROG05.h"
using namespace std;
int main()
{
	// initialize variables
	int firstNum, secondNum;
	//int sum;
	int number;
	//int value;
	//sum = 0;
	// get input from the user
	cout << "Please enter two numbers, first no. must be smaller than the second: ";
	// read (input)values into variables
	cin >> firstNum >> secondNum;
	// show what the input is
	cout << "You have entered " << firstNum << " and " << secondNum << ".";
	cout << endl << endl << endl;
   number = firstNum;
   if((number % 2) == 0) number++;
   while(number < secondNum)
   { 
	   cout << number << " "; 
   number += 2; 

   }
   cout << endl << endl << endl;
   
 
   /*value = firstNum;
   if ((value % 2) != 0) number ++;
   while (value < secondNum)
   {
      cout << value << " ";
   number += 2;
   }
    cout << endl << endl << endl;
*/
return 0;
}
Last edited by LuciWiz : 26-Jul-2005 at 10:01. Reason: Please insert your C++ code between [c++] & [/c++] tags; edited title
  #2  
Old 25-Jul-2005, 19:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
Sure can, if you read the guidelines. If all caps is considered shouting, is all bold considered yelling?

First while loop seems fine. Second is commented out so it won't be executed.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 25-Jul-2005, 19:13
amazingme001 amazingme001 is offline
New Member
 
Join Date: Jul 2005
Posts: 5
amazingme001 is on a distinguished road

...


Sorry about the bold, I actually just used ArialBold font, -- bolded without intention.
  #4  
Old 25-Jul-2005, 19:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
Quote:
Originally Posted by amazingme001
Sorry about the bold, I actually just used ArialBold font, -- bolded without intention.
Gee, maybe that's why it was called ArialBold??? ;-) You can stick with the standard font, it's quite adequate. By the way, welcome to GID...

Now what is it you need help with? And did you read the guidelines?
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 25-Jul-2005, 19:38
amazingme001 amazingme001 is offline
New Member
 
Join Date: Jul 2005
Posts: 5
amazingme001 is on a distinguished road
Question

C++ while loop


sorry I did not read the guidelines right away. (At least i admit it!) I was trying to solve my problem. At this point my program prints (at least some of it) odd numbers and gives me some kind of sum, but i see it is incorrect.
If user inputs 1 and 10 (for example) i need it to display
1 3 5 7 9
20
but it is not adding corect numbers
Can you help please, i feel I am so close to the solution, but just stuck. here is revised program now:

CPP / C++ / C Code:
//*************************************************************
// The following program  uses while loops to perform
// several steps. It prompts user to enter two numbers, 
//it ouputs the odd numbers between those numbers, outputs
// the sum of all even numbers found between the two numbers
// and finally it outputs all uppercase letters.
//************************************************************* 
#include <iostream>
#include <fstream>
#include <iomanip>

#include "PROG05.h"
using namespace std;
int main()
{
	// initialize variables
	int firstNum, secondNum;
	int sum;
	cout << "Enter two numbers: " << endl;
	cin >> firstNum >> secondNum;

	// following loop finds all the odd numbers and outputs them
	if ((firstNum % 2) ==0) firstNum++;
	while (firstNum < secondNum)
	{
		cout << firstNum << endl;
		firstNum +=2;
	}
    
	// this loop should find all even numbers and output the sum of those
	sum = 0;
	while (firstNum < secondNum)
	{
		if ((firstNum % 2) == 0) sum += firstNum;
		firstNum +=2;
	}
	cout << sum << endl;


	

return 0;
}
Last edited by LuciWiz : 26-Jul-2005 at 00:56. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #6  
Old 25-Jul-2005, 19:45
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
Quote:
Originally Posted by amazingme001
sorry I did not read the guidelines right away. (At least i admit it!)
And you still didn't. Or decided to ignore them.

Read at least guideline #1
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 25-Jul-2005, 19:51
amazingme001 amazingme001 is offline
New Member
 
Join Date: Jul 2005
Posts: 5
amazingme001 is on a distinguished road
i am sorry but I don't know what are you talking about. i read the guidelines and i also followed the rule!
  #8  
Old 25-Jul-2005, 20:29
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by amazingme001
i am sorry but I don't know what are you talking about. i read the guidelines and i also followed the rule!

This is a direct quote from the sticky thread (number 5566, nice and easy to remember).

1: When posting code, to help us read your posts please
  1. use code tags.
    1. Start with [c] or [c++]
    2. Then your code
    3. End with [/c] or [/c++]
    For additional information on code tags, see this announcement.
  2. Be sure you've formatted you code. Check out this information for tips and ideas.
  3. Post only the code that is relevant to the question. You may have to explain how some of the variables are defined.
  4. If your code is fairly large, can you
    1. cut out unnessesary sections?
    2. place comments around the section you need help with?
    Posting 200 lines of code with no indication of where we should look is not a good idea.



You want your code to end up looking like this.
CPP / C++ / C Code:
// my code with formatting intact
                // see how it stays where you put it

If you still don't see, hit reply to this post and look at the code tags. Liberally use the Preview Post button.

Mark

I'm getting out of here, don't want to be around for a drive by ... Otherwise, the code mashes against the left side and becomes unreadable, or at the very least, less likely to be read.
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #9  
Old 25-Jul-2005, 22:22
amazingme001 amazingme001 is offline
New Member
 
Join Date: Jul 2005
Posts: 5
amazingme001 is on a distinguished road

unbelievable


I got the solution! Anyhow, i hope you had fun lecturing new forum user!!!
  #10  
Old 26-Jul-2005, 01:02
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by amazingme001
I got the solution! Anyhow, i hope you had fun lecturing new forum user!!!

Hi amazingme001.

I am sorry you didn't get the help you were looking for sooner. This is, however, not the members' fault. They are all a bunch of great people, ready to help you in their time. The only thing we require is for new members such as yourself to obey the few rules we have on the Forum.
Since you were directed several times to the Guidelines, I don't know what else to say. Be more careful next time, use code tags for your code, and the help will come.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

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
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06

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

All times are GMT -6. The time now is 09:15.


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