Thread: is the program related to the question?

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    15

    is the program related to the question?

    Write a program that computes a running sum of inputs from the user terminating when the user gives an input value of 0.
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int x,y,z;
    do
    {
    cout<<"enter the 1st number:\n";
    cin>>x;
    cout<<"enter the 2nd number:\n";
    cin>>y;
    z=x+y;
    cout<<"the total is:"<<z<<'\n';
    }
    while(!(x==0 || y==0));
    cout<<"process terminated!";
    }
    i thought i wont be able to get the program to work if i used a for loop as i have to check if both x and y are 0. so i used a do while loop. thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    No, running sum mean you need to continue add numbers til 0 is entered
    Output should be something like this
    Code:
    Enter Number: 10
    Running sum: 10, enter next number: 5
    Running sum: 15, enter next number: 1
    Running sum: 16, enter next number: 3
    Running sum: 18, enter next number: 0
    
    Final sum 18, thank you for using our product.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by vart View Post
    No, running sum mean you need to continue add numbers til 0 is entered
    Output should be something like this
    Code:
    Enter Number: 10
    Running sum: 10, enter next number: 5
    Running sum: 15, enter next number: 1
    Running sum: 16, enter next number: 3
    Running sum: 18, enter next number: 0
    
    Final sum 18, thank you for using our product.
    thanks .let me try it

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by vart View Post
    No, running sum mean you need to continue add numbers til 0 is entered
    Output should be something like this
    Code:
    Enter Number: 10
    Running sum: 10, enter next number: 5
    Running sum: 15, enter next number: 1
    Running sum: 16, enter next number: 3
    Running sum: 18, enter next number: 0
    
    Final sum 18, thank you for using our product.
    is this ok?
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int num1,next_numbers,total;
    cout<<"enter the number:\n";
    cin>>num1;
    cout<<"\nenter next number:\n";
    cin>>next_numbers;
    total=num1+next_numbers;
    cout<<"sum is at:\n"<<total;
    for(next_numbers;next_numbers!=0;)
    {
    cout<<"\nenter next number:\n";
    cin>>next_numbers;
    total=total+next_numbers;
    cout<<"sum is at:\n"<<total;
    }
    }
    Last edited by recklezz; 04-16-2015 at 08:46 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code properly and use proper variable names. x, y, z does not tell us much what they're used for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Please indent your code properly and use proper variable names. x, y, z does not tell us much what they're used for.
    is this good?..i replaced the x,y and z.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better, but you still haven't indented properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It is a bit better, but:
    1. Does your program close when the user enters 0 as the first number? Why it does/does not?
    2. Try to remove duplication of code. E.g., in your current solution there are at least 4 lines that are needlessly repeated.
    3. Indent as already has been suggested. This will help you to better understand workflow of the code.

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by kmdv View Post
    It is a bit better, but:
    1. Does your program close when the user enters 0 as the first number? Why it does/does not?
    2. Try to remove duplication of code. E.g., in your current solution there are at least 4 lines that are needlessly repeated.
    3. Indent as already has been suggested. This will help you to better understand workflow of the code.
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int num1,next_numbers,total;
    cout<<"enter the number:\n";
    cin>>num1;
    cout<<"\nenter next number:\n";
    cin>>next_numbers;
    if(!(num1==0||next_numbers==0)) //checks if the entered values are not zero
    {
    total=num1+next_numbers;
    cout<<"sum is at:\n"<<total;
    for(next_numbers;next_numbers!=0;) //asks the user continuous input until he enters zero
    {
    cout<<"\nenter next number:\n";
    cin>>next_numbers;
    total=total+next_numbers;
    cout<<"sum is at:\n"<<total;
    }
    }
    }
    1.now it closes if the first input is o .
    2.i couldn't find the extra lines looks like every line is necessary.

    @Elysia is it better?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you have not indented properly still. The code is even more unreadable than before.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by Elysia View Post
    No, you have not indented properly still. The code is even more unreadable than before.
    i added a if statement.how to better indent the code??
    i don't get it

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by recklezz View Post
    i added a if statement.how to better indent the code??
    i don't get it
    Read: Indent style - Wikipedia, the free encyclopedia
    Then start using your tab key.

  13. #13
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by Matticus View Post
    Read: Indent style - Wikipedia, the free encyclopedia
    Then start using your tab key.
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
        int num1,next_numbers,total;
        cout<<"enter the number:\n";
        cin>> num1;
        cout<<"\nenter next number:\n";
        cin>> next_numbers;
        if(!(num1==0 || next_numbers==0))         //checks if the entered values are not zero
            {
                total=num1+next_numbers;
                cout<<"sum is at:\n"<<total;
                for(next_numbers;next_numbers!=0;) //asks the user continuous input until he enters zero
                    {
                        cout<<"\nenter next number:\n";
                        cin>>next_numbers;
                        total=total+next_numbers;
                        cout<<"sum is at:\n"<<total;
                    }
            }       
    }
    is this ok?
    thanks matticus

  14. #14
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by recklezz View Post
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
        int num1,next_numbers,total;
        cout<<"enter the number:\n";
        cin>> num1;
        cout<<"\nenter next number:\n";
        cin>> next_numbers;
        if(!(num1==0 || next_numbers==0))         //checks if the entered values are not zero
            {
                total=num1+next_numbers;
                cout<<"sum is at:\n"<<total;
                for(next_numbers;next_numbers!=0;) //asks the user continuous input until he enters zero
                    {
                        cout<<"\nenter next number:\n";
                        cin>>next_numbers;
                        total=total+next_numbers;
                        cout<<"sum is at:\n"<<total;
                    }
            }       
    }
    is this ok?
    thanks matticus
    Your formatting looks acceptable, one nit-pick though - you could have:

    Code:
    total = total + next + next_number
    You are free to chose your formatting style - but most formatting styles will look good as long as your remain consistent - for instance 'clang-format' gives me:

    Code:
    #include <iostream>
    using namespace std;
    int main() {
      int num1, next_numbers, total;
      cout << "enter the number:\n";
      cin >> num1;
      cout << "\nenter next number:\n";
      cin >> next_numbers;
      if (!(num1 == 0 ||
            next_numbers == 0))  // checks if the entered values are not zero
      {
        total = num1 + next_numbers;
        cout << "sum is at:\n" << total;
        for (next_numbers;
             next_numbers !=
                 0;)  // asks the user continuous input until he enters zero
        {
          cout << "\nenter next number:\n";
          cin >> next_numbers;
          total = total + next_numbers;
          cout << "sum is at:\n" << total;
        }
      }
    }
    That is the coding style used in most google projects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with first c++ program (math-related)
    By necrovamp in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 12:33 PM
  2. A graphics related question
    By GOBLIN-85 in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2008, 01:37 PM
  3. A question related to BGI
    By progue in forum Windows Programming
    Replies: 6
    Last Post: 05-25-2008, 10:04 AM
  4. Dos related Question
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-02-2002, 12:16 PM
  5. non program related questions about C
    By Nova in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 03:42 PM

Tags for this Thread