Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,135 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,809 people online right now. Registration is fast and FREE... Join Now!




FOR loops + sum

 
Reply to this topicStart new topic

FOR loops + sum, help plz...

noob2007
11 Apr, 2007 - 08:59 PM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
tis does not seem to work

CODE
     int num1, num2, total;
    

    
    cout << "\nEnter your first number: ";
    cin >> num1;
    cout << "\nEnter your second number: ";
    cin >> num2;
    cout << "\nYour range of numbers are: \n";
    
    for (int num = num1 + 1; num <= num2 - 1; num++)
    {
        
        cout << "\n"  << num;
        cout << "\n" << total;

       }

                   total =   ((num2-1) * (num1+1)) + 13;
                   cout << "\n" << total;
       cout << "\n\n";

    system("pause");
    return 0;
}


This post has been edited by noob2007: 11 Apr, 2007 - 10:29 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: FOR Loops + Sum
11 Apr, 2007 - 09:56 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
One of the problems is that you never initialize your variable total before you start using it. So you are bound to get unpredictable results in your output. It is always a good idea to initialize your variables.

You might also want to display the value of total after the loop has completed iterating.

CODE

int main()
{
     int num1, num2, total = 0;
    

    cout << "This program will display a range of numbers according to what you have\ninputted\n";
    cout << "\nEnter your first number: ";
    cin >> num1;
    cout << "\nEnter your second number: ";
    cin >> num2;
    cout << "\nYour range of numbers are: \n";
    
      
    for (int num = num1 + 1; num <= num2 - 1; num++)
    {
        total = total + num;
        cout << "\t" << setw(3) << num;        
        
       }
      
       cout << "\n" << setw(3) << total;
      
       cout << "\n\n";

    system("pause");
    return 0;
}

User is offlineProfile CardPM
+Quote Post

noob2007
RE: FOR Loops + Sum
11 Apr, 2007 - 10:01 PM
Post #3

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
QUOTE(jayman9 @ 11 Apr, 2007 - 10:56 PM) *

One of the problems is that you never initialize your variable total before you start using it. So you are bound to get unpredictable results in your output. It is always a good idea to initialize your variables.

You might also want to display the value of total after the loop has completed iterating.

CODE

int main()
{
     int num1, num2, total = 0;
    

    
    cout << "\nEnter your first number: ";
    cin >> num1;
    cout << "\nEnter your second number: ";
    cin >> num2;
    cout << "\nYour range of numbers are: \n";
    
      
    for (int num = num1 + 1; num <= num2 - 1; num++)
    {
        total = total + num;
        cout << "\t" << setw(3) << num;        
        
       }
      
        
        cout << "\n" << total;
       cout << "\n\n";

    system("pause");
    return 0;
}





hmm.. i 4got to initialise total. and i had no idea how to add the numbers but thanks man
icon_up.gif great help

This post has been edited by noob2007: 11 Apr, 2007 - 10:30 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month