Thread: For/Next Loops...adding 10 numbers...

  1. #1
    Registered User IanelarAzure's Avatar
    Join Date
    Sep 2002
    Posts
    7

    Question For/Next Loops...adding 10 numbers...

    I have an assignment in programming class, that degrades what I know and can do. I have to make a program using For/Next loops and it has to ask for a name, display the name, then ask for 10 numbers, add them, and get the average, displaying both sum and average. My program works...but it doesnt add the numbers. Dont worry about the average part, not there yet. For/Next loops are killing me....way behind my times. :P Here is my code:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
    	string name;
    	double num[10],sum;
    	int i;
    	cout<<"Please enter your name: "<<endl;
    	cin>>name;
    	cout<<"Your name is "<<name<<endl;
    	for(i=0,i<10;i++;){
    	cout<<"Please enter a number: ";
    	cin>>num[i];
    	sum=i+num[10];
    	cout<<"The sum of your numbers equals "<<sum<<endl;
    	}
    	return(0);
    }
    I know my problem lies in the for statement...but i just cant seem to understand it. Thank you.
    Last edited by IanelarAzure; 09-11-2002 at 09:39 PM.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Look closely at what you're doing here.
    Code:
    sum=i+num[10];
    what you are doing there is over writing the value of sum every time with the value of num[10]+i, and since num[10] exceeds the bounds of your array it contains garbage.

    you declared an array of 10 doubles, that means you have:
    num[0] through num[9]. there is no num[10].

    also you are just incrimenting the garbage by adding the value of i.

    what you want is something like this.

    Code:
    //initialize sum to 0
    sum=0;
    
    for(i=0,i<10;i++;)
    {
      cout<<"Please enter a number: ";
      cin>>num[i];
    
      //this is the correction
      sum+=num[i];
    
      cout<<"The sum of your numbers equals "<<sum<<endl;
    }
    what i'm doing above is making sure that i start off with 0 as the value of sum. then for each iteration of the loop i load the user's input into the current indice in the num array. next i add whatever the value of num[current indice] is to sum. so in it would be something like this:

    ***pseudo code***
    1st time through the loop
    sum now equals 0
    user inputs 5 into num[0]
    sum equals 0 plus 5

    2nd time through the loop
    sum now equals 5
    user inputs 3 into num[1]
    sum equals 5 plus 3

    3rd time through the loop
    sum now equals 8
    user inputs 2 into num[2]
    sum equals 8 plus 2

    etc. etc. etc.

    hope this helps.

    the average part is really simple also.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by IanelarAzure
    Well, I fully understand that part....but now the loop itself isnt running.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
    	string name;
    	double sum,num[10];
    	int i;
    	cout<<"Please enter your name: "<<endl;
    	cin>>name;
    	cout<<"Your name is "<<name<<endl;
    	sum=0;
    	for(i=0,i<10;i++;){
    	cout<<"Please enter a number: ";
    	cin>>num[i];
    	sum+=num[i];
    	cout<<"The sum of your numbers equals "<<sum<<endl;
    	}
    	return(0);
    }
    I even tried making "i=0" before the loop, didnt work. Im sure this will work, but cant figure out why the loop isnt running at all.

    Tracing:

    Please enter your name:
    Daniel
    Your name is Daniel
    Press any key to continue_

    Thanks for the loop help though, very appreciated.
    Look carefully at your loop declaration it should be like this...

    for( i = 0; i < 10; i++ )

    NOT

    for( i = 0, i < 10; i++; )

    try this code to clear it up.
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
      string name;
      float fNum;
      float fSum = 0.0f;
      int i;
    
      cout << "Please enter your name: " << endl;
      cin >> name;
      cout << "Your name is "<< name << endl;
      
      /* Our for loop */
      for( i = 0; i < 10; i++ )
      {
        cout << "Please enter a number: ";
        cin >> fNum;
        fSum += fNum;
        cout<<"The sum of your numbers equals "<< fSum << endl;
      }
      
      return 0;
    }

  4. #4
    Registered User IanelarAzure's Avatar
    Join Date
    Sep 2002
    Posts
    7
    Had a breakthrough...

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
    	string name;
    	double sum,num[10];
    	int i;
    	sum=0
    	cout<<"Please enter your name: "<<endl;
    	cin>>name;
    	cout<<"Your name is "<<name<<endl;
    	for(i=0,i<10;i+1;){
    	cout<<"Please enter a number: ";
    	cin>>num[i];
    	}
                    sum+=num[i];
    	cout<<"The sum of your numbers equals "<<sum<<endl;
    	return(0);
    }
    This lets the loop run, but it never stops asking for numbers. Wouldnt it do that after it reaches num[9]?? Well, I also moved the whole sum statement outside the loop so it wont display the sum everytime they input a number. But why does it still loop infinitely?

  5. #5
    Registered User IanelarAzure's Avatar
    Join Date
    Sep 2002
    Posts
    7
    Ok....my program fully works. I edited a little, the placement of the semicolons is what the problem was after myy breakthrough. Here is the finished piece:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
         string name, selection;
         double sum,num[10],mean;
    do{
         int i;
         cout<<"Please enter your name: "<<endl;
         cin>>name;
         cout<<"Your name is "<<name<<endl;
         for(i=0;i<10;i++){
         cout<<"Please enter a number: ";
         cin>>num[i];
         }
         sum=num[0]+num[1]+num[2]+num[3]+num[4]+num[5]+num[6]+num[7]+num[8]+num[9];
         cout<<"The sum of your numbers equals "<<sum<< endl;
         mean=sum/10;
         cout<<"The average of these numbers is "<<mean<<endl;
         cout<<"Would you like to repeat this program? ";
         cin>>selection;
    }while(selection=="Yes" || selection=="yes");
         return(0);
    }
    Thank you both for the help...very much needed. I hope to be able to help others soon from now.^_^

  6. #6
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Hey sup,

    IanelarAzure,you should use a for loop to add them up.
    Have a look:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    	string name, selection;
    	double sum = 0,num[10],mean = 0;
    	do{
    		int i;
    		cout<<"Please enter your name: "<<endl;
    		cin>>name;
    		cout<<"Your name is "<<name<<endl;
    		for(i=0;i<10;i++)
    		{
    			cout<<"Please enter a number: ";
    			cin>>num[i];
    		}
    		for(i=0;i<10;i++)
    			sum+=num[i];
    		cout<<"The sum of your numbers equals "<<sum<< endl;
    		mean=sum/10;
    		cout<<"The average of these numbers is "<<mean<<endl;
    		cout<<"Would you like to repeat this program? ";
    		cin>>selection;
    	}while(selection=="Yes" || selection=="yes");
        return(0);
    }
    Hope this helps

    Marc
    Last edited by marCplusplus; 09-12-2002 at 12:05 PM.
    No matter how much you know, you NEVER know enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding numbers with arrays
    By Oliveira in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 10:25 PM
  2. Adding line numbers to file
    By getout in forum C Programming
    Replies: 6
    Last Post: 01-04-2007, 12:31 PM
  3. adding large numbers
    By derek23 in forum C Programming
    Replies: 6
    Last Post: 07-22-2005, 08:02 PM
  4. Adding double numbers in a function
    By ybala in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:36 AM
  5. Replies: 7
    Last Post: 01-02-2002, 09:16 PM