Problems with my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    Problems with my code

    Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

    Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
    It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

    This is what I have so far

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(){
        int i,;
        double n, m, o, p, q, r, s, t;
        double scores[7];
        scores[0] = n;
        scores[1] = m;
        scores[2] = o;
        scores[3] = p;
        scores[4] = q;
        scores[5] = r; 
        scores[6] = s;
        
        cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
        cin >> n >> m >> o >> p >> q >> r >> s;
        for(i = 0; i < 7; i++){
              cout << scores[i] <<", ";
              
              }
              cout << "\n The total sum =" << n + m + o + p + q + r + s;
              system("PAUSE");
              return 0;
              }
    I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


    Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

    Can I get a few tips ? Also, any tips for those for loops It says to write ?

    I appreciate your time
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    #2
    Originally posted by Hunderpanzer
    Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

    Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
    It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

    This is what I have so far

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(){
        int i,;
        double n, m, o, p, q, r, s, t;
        double scores[7];
        scores[0] = n;
        scores[1] = m;
        scores[2] = o;
        scores[3] = p;
        scores[4] = q;
        scores[5] = r; 
        scores[6] = s;
        
        cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
        cin >> n >> m >> o >> p >> q >> r >> s;
        for(i = 0; i < 7; i++){
              cout << scores[i] <<", ";
              
              }
              cout << "\n The total sum =" << n + m + o + p + q + r + s;
              system("PAUSE");
              return 0;
              }
    I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


    Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

    Can I get a few tips ? Also, any tips for those for loops It says to write ?

    I appreciate your time
    When I compiled the program I received various errors. I think the problem is that you are not initializing your array to zeroes. For example, you're assigning score[0] the value n. What is the value of n? Also you are not storing any values in the array, that's where your other for loop would come in to play.
    J

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Hunderpanzer
      Hi, I'm pretty new here. I've started programming C++ on and off now for a while now, but never got anywhere with it. So I decided to stick with my book examples and see where it takes me.

      Okay my goal is to write a program that prompts the user for 7 values, stores them in an array, and then prints each of them out, and their sum.
      It also notes that I need two for loops to be included; one for collecting the inputs, and one for calculating the sum and printing the value.

      This is what I have so far

      Code:
      #include <iostream>
      #include <math.h>
      using namespace std;
      
      int main(){
          int i,;
          double n, m, o, p, q, r, s, t;
          double scores[7];
          scores[0] = n;
          scores[1] = m;
          scores[2] = o;
          scores[3] = p;
          scores[4] = q;
          scores[5] = r; 
          scores[6] = s;
          
          cout << "Enter seven numbers for me to store. I will then\n print each out, and their total\n";
          cin >> n >> m >> o >> p >> q >> r >> s;
          for(i = 0; i < 7; i++){
                cout << scores[i] <<", ";
                
                }
                cout << "\n The total sum =" << n + m + o + p + q + r + s;
                system("PAUSE");
                return 0;
                }
      I tried initializing the array values with user inputted values on the same line of code, but it gave me a funky error.


      Okay, so far the program takes everything in, and after entering the 7th number, it spits out Random letters, numbers... I've seen this happen when trying to do math with Letters, but I don't see how it's adding letters...they should have value.

      Can I get a few tips ? Also, any tips for those for loops It says to write ?

      I appreciate your time
      I think your understanding about loops and arrays is broken. Here's a little
      trick:

      1) get rid of that array;
      2) rename your variables n, o, ... t to s0, s1, s2, ... s6;
      3) rewrite your algorithm without using loops or arrays;
      4) notice all that repeating code;
      5) define an array 'double s[7];'
      6) use variables s[0], s[1] ... s[6] instead of s0, s1, ... s6
      7) get rid of the declarations of s0, s1, ... s6

      At this stage you're using an array of seven doubles; you still should observe 4)
      Now try to bring those loops back in; you did fairly well with them in your
      original version of the code.

      best of luck and

      kind regards,

      Jos

      Comment

      • Hunderpanzer
        New Member
        • Jul 2007
        • 60

        #4
        I figured it out !

        Thanks to both of you, and the help you've given me!

        Now my code looks like this:
        Code:
         
        #include <iostream>
        #include <math.h>
        using namespace std;
        
        int main(){
            int i, b;
            double s0, s1, s2, s3, s4, s5, s6;
            double s[7] = {s0, s1, s2, s3, s4, s5, s6};
            
            cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
            for(b = 0; b < 7; b++){
                  cin >> s[b];
                  }
            for(i = 0; i < 7; i++) { 
                  cout << s[i] <<" ";
                  }
            cout << "The total sum = " << s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] << "!" << endl;
                  system("PAUSE");
                  return 0;
                  }
        I got the 2 loops in for collecting, and printing the numbers input, but I can't seem to figure out how to squeeze in the total sum into the loop...

        Would I have to declare and define a new function in order for it to be easier on the eyes ?

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by Hunderpanzer
          I figured it out !

          Thanks to both of you, and the help you've given me!

          Now my code looks like this:
          Code:
           
          #include <iostream>
          #include <math.h>
          using namespace std;
          
          int main(){
              int i, b;
              double s0, s1, s2, s3, s4, s5, s6;
              double s[7] = {s0, s1, s2, s3, s4, s5, s6};
              
              cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
              for(b = 0; b < 7; b++){
                    cin >> s[b];
                    }
              for(i = 0; i < 7; i++) { 
                    cout << s[i] <<" ";
                    }
              cout << "The total sum = " << s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] << "!" << endl;
                    system("PAUSE");
                    return 0;
                    }
          I got the 2 loops in for collecting, and printing the numbers input, but I can't seem to figure out how to squeeze in the total sum into the loop...

          Would I have to declare and define a new function in order for it to be easier on the eyes ?
          You dont need to declare all these extra double variables. Your array is already 7 double variables. For the sum, what you need is a sum variable and you adding the value to the sum each iteration of the for loop.
          [code=cpp]
          #include <iostream>
          #include <math.h>
          using namespace std;

          int main(){
          int i, b;
          double s[7] = {0}; // initialize all members to 0

          cout << "Enter numbers you want me to store, and print out, and then I'll add 'em all up!" ;
          for(b = 0; b < 7; b++)
          {
          cin >> s[b];
          }

          int sum = 0;
          for(i = 0; i < 7; i++)
          {
          cout << s[i] <<" ";
          sum += s[i]; // add value to sum
          }
          cout << "The total sum = " << sum << "!" << endl;
          system("PAUSE") ;
          return 0;
          }[/code]

          Comment

          • Hunderpanzer
            New Member
            • Jul 2007
            • 60

            #6
            Ahh, now I see.

            I just realized that the book clearly defines the operator += a few chapters back (which I had forgotten about).

            Thanks for the help, it worked out perfectly !

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              Originally posted by Hunderpanzer
              Ahh, now I see.

              I just realized that the book clearly defines the operator += a few chapters back (which I had forgotten about).

              Thanks for the help, it worked out perfectly !
              You're welcome, I'm glad to help.

              Comment

              Working...