Thread: Help with array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    Help with array

    I have this code and I am trying to make it look like this:
    Code:
    How many values are you going to enter (5-25)? 30
    Too many values; try again.
    How many values are you going to enter (5-25)? 2
    Too few values; try again.
    How many values are you going to enter (5-25)? 8
    
    Please enter value 1: 12.2
    Please enter value 2: 7.6
    Please enter value 3: -200.3
    Please enter value 4: 140.7
    Please enter value 5: 890.23
    Please enter value 6: 67.78
    Please enter value 7: 99.9
    Please enter value 8: 100.1
    I can get the first part as in how many values you want to enter which i can get until I add the next part into it which is Please enter value. When I add this is a for loop it messes it up.

    This is my code when I just add the first part
    Code:
     
      numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    while (numEntered < 5 || numEntered > 25 ) {
    if (numEntered < 5) {
    cout << "To Few values; try again" << endl; 
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    else if (numEntered > 25) {
    cout << "To many values; try again" << endl;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    and this is what i get:
    Code:
    How many values are you going to enter (5-25)30
    To many values; try again
    How many values are you going to enter (5-25)2
    To Few values; try again
    How many values are you going to enter (5-25)8
    This on top looks good but when i add the next part, it misses up:
    This is my new code:
    Code:
    numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    while (numEntered < 5 || numEntered > 25 ) {
    if (numEntered < 5) {
    cout << "To Few values; try again" << endl; 
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    else if (numEntered > 25) {
    cout << "To many values; try again" << endl;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    
    
    for (i=1; i <= numEntered; i++) {
    cout << "Please enter value " << i <<  ": ";
    cin >> A[i];
    }
    It makes it look like this:

    Code:
    How many values are you going to enter (5-25)30
    To many values; try again
    How many values are you going to enter (5-25)2
    Please enter value 1: 8
    Please enter value 2: 12.2
    To Few values; try again
    How many values are you going to enter (5-25)To Few values; try again
    How many values are you going to enter (5-25)7.6
    Please enter value 1: -200.3
    Please enter value 2: 140.7
    Please enter value 3: 890.23
    Please enter value 4: 67.78
    Please enter value 5: 99.9
    Please enter value 6: 100.1
    Please enter value 7:
    Can you tell me what I am doing wrong
    Last edited by walkonwater; 04-24-2009 at 08:21 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Please help

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You are missing a brace to close the while loop. If you indented your code correctly you would have saw that.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Ok I fixed the brace but still not luck

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What's your new correctly indented code?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Code:
    numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    while (numEntered < 5 || numEntered > 25 ) {
    if (numEntered < 5) {
    cout << "To Few values; try again" << endl; 
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    else if (numEntered > 25) {
    cout << "To many values; try again" << endl;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    } 
    }
    
    cout << endl << endl;
    This part works right for what I want it to do but when I add the rest of it, it screws up. I don't know what I am doing wrong? It is only a little mistake but it adds another output.

    Code:
     
      numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    while (numEntered < 5 || numEntered > 25 ) {
    if (numEntered < 5) {
    cout << "To Few values; try again" << endl; 
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    else if (numEntered > 25) {
    cout << "To many values; try again" << endl;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    } 
    }
    
    cout << endl << endl;
    
    for (j=0; j <= numEntered; j++){
    cout << "Please enter value ";
        cin >> A[j];
    }
    This above code looks like this:

    Code:
    How many values are you going to enter (5-25)30
    To many values; try again
    How many values are you going to enter (5-25)2
    To Few values; try again
    How many values are you going to enter (5-25)8
    
    Please enter value 12.2
    Please enter value 7.6
    Please enter value -200.3
    Please enter value 140.7
    Please enter value 890.23
    Please enter value 67.78
    Please enter value 99.9
    Please enter value 100.1
    Please enter value

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Ok guys I did it,I just changed j =1

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    but now I am trying to find the average and sum and also the average of numbers over 100.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    This is my new code but for some reason it gives me an outrageous number for sum
    Code:
     numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    while (numEntered < 5 || numEntered > 25 ) {
    if (numEntered < 5) {
    cout << "To Few values; try again" << endl; 
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    }
    else if (numEntered > 25) {
    cout << "To many values; try again" << endl;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    } 
    }
    
    cout << endl << endl;
    
    for (j=1; j <= numEntered; j++){
    cout << "Please enter value ";
        cin >> A[j];
    }
    
    cout << endl << endl;
    
    sum = 0.0;
    for (j=0; j < 10; j++) {
        sum += A[j];
        }
        
        cout << "The sum is " << sum;

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    correctly indented
    If you aren't willing to spend a few seconds fixing your indentation and make your code a lot easier to read, why should we spend our time helping you?

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Is this better?
    Code:
    numEntered = 0;
      cout << "How many values are you going to enter (5-25)";
       cin >> numEntered;
    
    
    while (numEntered < 5 || numEntered > 25 ) {
    
    if (numEntered < 5) {
      cout << "To Few values; try again" << endl; 
      cout << "How many values are you going to enter (5-25)";
      cin >> numEntered;
    }
    
    else if (numEntered > 25) {
      cout << "To many values; try again" << endl;
      cout << "How many values are you going to enter (5-25)";
      cin >> numEntered;
    } 
    }
    
      cout << endl << endl;
    
    for (j=1; j <= numEntered; j++){
      cout << "Please enter value ";
      cin >> A[j];
    }
    
      cout << endl << endl;
    
    sum = 0.0;
    for (j=0; j < numEntered; j++) {
        sum = sum + A[j];
        }
        
        cout << "The sum is " << sum;

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    numEntered = 0;
    cout << "How many values are you going to enter (5-25)";
    cin >> numEntered;
    
    while (numEntered < 5 || numEntered > 25 ) {
    
    	if (numEntered < 5) {
    		cout << "To Few values; try again" << endl; 
    		cout << "How many values are you going to enter (5-25)";
    		cin >> numEntered;
    	}
    
    	else if (numEntered > 25) {
    		cout << "To many values; try again" << endl;
    		cout << "How many values are you going to enter (5-25)";
    		cin >> numEntered;
    	} 
    	
    }
    
    cout << endl << endl;
    
    for (j=1; j <= numEntered; j++){
    	cout << "Please enter value ";
    	cin >> A[j];
    }
    
    cout << endl << endl;
    
    sum = 0.0;
    for (j=0; j < numEntered; j++) {
    	sum = sum + A[j];
    }

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Anyways since you corrected the "indention" why is my sum not working right?

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What is your input? What is the output? What is the expected output?

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    This is what is wrong:
    Code:
    How many values are you going to enter (5-25)30
    To many values; try again
    How many values are you going to enter (5-25)2
    To Few values; try again
    How many values are you going to enter (5-25)8
    
    Please enter value 12.2
    Please enter value 7.6
    Please enter value -200.3
    Please enter value 140.7
    Please enter value 890.23
    Please enter value 67.78
    Please enter value 99.9
    Please enter value 100.1
    
    The sum is: 66550

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM