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 24-Oct-2009, 02:21
saqib_ saqib_ is offline
New Member
 
Join Date: Oct 2009
Posts: 12
saqib_ has a little shameless behaviour in the past

1 loop structure to find the sum of odd and even integers


Hello, I am new to this forum and to programming. I was trying to make a small program that calculates the sum of odd and even integers up to the limit provided by the user. The code is given below. The problem is to find the even sum. I am stuck at this point. Please guide me.

CPP / C++ / C Code:
/* 
   This program calculates the sum of odd and even integers upto a given
   limit by the user using one loop structure only. 
*/

// ---> Start program

#include <iostream.h>
#include <conio.h>

int main()
{
    // ---> Declare variables and thier values
    
    int oddsum(0), num(1), evensum(0), userlim;
    
    // ---> Get user input
    
    cout << "Please enter the limit : ";
    cin  >> userlim;
    
    // ---> Calculate the sum of odd and even integers upto the limit
    
    // ---> Using while loop
    
    while(num<=userlim)
    {
                       oddsum+=num;
                       num+=2;
    }
    
    // ---> Show the result
    
    cout << "The sum of odd integers upto " << userlim << " is : " << oddsum << "\n";
    cout << "The sum of even integers upto " << userlim << " is : " << evensum;
    
    // End program
    
    getch();
    return 0;
    
}

Last edited by admin : 24-Oct-2009 at 05:02. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 24-Oct-2009, 07:41
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


There is no standard header named conio.h...don't use it unless required by your course/instructor...and then question why you must use it.

I don't think that I'd use a while conditional; rather a for seems more suitable:

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

using namespace std;

int main()
{
    int userlimit;
    cout << "Enter an integer limit: ";
    cin >> userlimit;

    if(userlimit < 2)
    {
	cout << "...not much to do!" << endl;
	exit(0);
    }

    int oddsum = 1;
    int evensum = 0;

    for(int i = 2; i <= userlimit; i++)
    {
	if(i % 2 == 0)
	{
	    evensum += i;
	}
	else
	{
	    oddsum += i;
	}
    }
    cout << "Sum of odd  integers between 1 and " << userlimit << " = " << oddsum << endl;	
    cout << "Sum of even integers between 1 and " << userlimit << " = " << evensum << endl;	

    return 0;
}




MxB
  #3  
Old 24-Oct-2009, 08:00
saqib_ saqib_ is offline
New Member
 
Join Date: Oct 2009
Posts: 12
saqib_ has a little shameless behaviour in the past

Re: 1 loop structure to find the sum of odd and even integers


Thank you for your guidance. You said that conion.h is not a standard header. I have used your code but the window disappears so fast that I cannot see the output. However if I use conio.h with getch(); in the end I can view my output of the program. Is it wrong to use it and write the code as you did in the example?
  #4  
Old 24-Oct-2009, 09:13
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


Quote:
Originally Posted by saqib_
Thank you for your guidance. You said that conion.h is not a standard header. I have used your code but the window disappears so fast that I cannot see the output. However if I use conio.h with getch(); in the end I can view my output of the program. Is it wrong to use it and write the code as you did in the example?

"Wrong" is relative. There are alternatives to using non-standard libraries and headers:

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

using namespace std;

int main()
{
    int userlimit;
    cout << "Enter an integer limit: ";
    cin >> userlimit;

    if(userlimit < 2)
    {
	cout << "...not much to do!" << endl;
	exit(0);
    }

    int oddsum = 1;
    int evensum = 0;

    for(int i = 2; i <= userlimit; i++)
    {
	if(i % 2 == 0)
	{
	    evensum += i;
	}
	else
	{
	    oddsum += i;
	}
    }
    cout << "Sum of odd  integers between 1 and " << userlimit << " = " << oddsum << endl;	
    cout << "Sum of even integers between 1 and " << userlimit << " = " << evensum << endl;	

    cout << "Type a key and then press Enter to continue...";
    char key;
    cin >> key;
    

    return 0;
}

	


MxB
  #5  
Old 26-Oct-2009, 21:10
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


You can also pause with this without having to use a variable:
CPP / C++ / C Code:
  cout << "Press return to continue ";
  cin.get();
Last edited by Howard_L : 26-Oct-2009 at 21:47.
  #6  
Old 27-Oct-2009, 04:11
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


Quote:
Originally Posted by Howard_L
You can also pause with this without having to use a variable:
CPP / C++ / C Code:
  cout << "Press return to continue ";
  cin.get();

I'm not sure that this is true in all cases on all platforms. For example, the following change:

CPP / C++ / C Code:
/*
    cout << "Type a key and then press Enter to continue...";
    char key;
    cin >> key;
    
*/
    cout << "Press return to continue...";
    cin.get();


...just ran without pausing, as shown below:

Code:
bash-3.2$ ./saqlib Enter an integer limit: 200 Sum of odd integers between 1 and 200 = 10000 Sum of even integers between 1 and 200 = 10100 Press return to continue...bash-3.2$

...on Mac OS X 10.5.8 using GNU GCC.


MxB
  #7  
Old 27-Oct-2009, 08:12
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


Quote:
Originally Posted by Howard_L
You can also pause with this without having to use a variable:
CPP / C++ / C Code:
  cout << "Press return to continue ";
  cin.get();
The difference between this and what Bob showed is that this will always read the next character, whereas Bob's version discards whitespace. And when people do stuff like
CPP / C++ / C Code:
int anInt;
cout << "Enter an int: ";
cin >> anInt;
there will a newline waiting in the input stream ready to be read by cin.get(). I'm not saying Bob's solution is better in my opinion, just that
CPP / C++ / C Code:
cin.get();
is not equal to
CPP / C++ / C Code:
cin >> aChar;
  #8  
Old 27-Oct-2009, 09:33
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


Ah yes, the leftover '\n' and extra characters typed. Good point M.
And nice explaination Kimmo.

Well I guess you could "flush" stdin after user input like you should do any time you want to get meaningful subsequent user input.
CPP / C++ / C Code:
    cin >> userlimit;
    while( (cin.get()) != '\n');    // empty remaining chars from cin (stdin)
    //blah blah
    cout << "Press return to continue ";
    cin.get();
That works for me , does that work on your platform too?
My platform is pine flooring on 2x4 joists about 12" above dirt... (gcc / fc6-linux)
  #9  
Old 27-Oct-2009, 13:21
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


CPP / C++ / C Code:
    cin >> userlimit;
    while( (cin.get()) != '\n');    // empty remaining chars from cin (stdin)
    //blah blah
    cout << "Press return to continue ";
    cin.get();

Perhaps, but then you need to be sure every time there actually is a newline. An alternative is to use getline and stringstreams.

CPP / C++ / C Code:
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::istringstream;
    using std::string;
    
    int anInt(0);
    string input;
    
    cout << "Enter some input (a small integer could be a good idea): ";
    getline(cin, input);
    
    istringstream inputStream(input);
    inputStream >> anInt;
    
    cout << endl;
    
    if (!inputStream.fail() && inputStream.eof())
        cout << "Great." << endl;
    else
        cout << input << " is probably not an integer" << endl;
        
    cout << endl << "Press Enter to continue...";
        
    cin.get();
    
    return 0;
}

Meaningless output:
Code:
Enter some input (a small integer could be a good idea): Talo Talo is probably not an integer Press Enter to continue...
  #10  
Old 27-Oct-2009, 14:27
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: 1 loop structure to find the sum of odd and even integers


Wow, that's an interesting way of handling user input.
You mean there is a way for user to enter data without pressing the Enter key?
Doesn't this always leave at least the '\n' like scanf()?
CPP / C++ / C Code:
  cin << datatype; 
 
 

Recent GIDBlogAccepted for Ph.D. program 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

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

All times are GMT -6. The time now is 00:27.


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