Find the answer to your Linux question:
Results 1 to 5 of 5
Before you yell at me for bad variable names, this is just one random practice assignment in school >_> Okay here is the code: #include "stdafx.h" #include <iostream> #include <iomanip> ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    32

    odd error?


    Before you yell at me for bad variable names, this is just one random practice assignment in school >_>

    Okay here is the code:


    #include "stdafx.h"

    #include <iostream>
    #include <iomanip>
    using namespace std;

    void main()
    {
    int x[100];
    int y,z,l;
    float sum,avg; // y is for the first loop, z count the amount of numbers, l is the second loop, sum is the sum
    char quit; // are you quiting or not

    for (y=1;y<=100;y++) {
    x[y]=0;
    }

    z=0;

    for (y=1;y<=100;y++) {
    z=z+1;
    cout << "Enter a number: ";
    cin >> x[y];
    cout << endl << "Was that the last number? (y for YES): ";
    cin >> quit;
    if (quit=='y' || quit=='Y')
    break;
    }


    sum = 0;

    for (l=1;l<=z;l++) {
    sum = sum + x[l];
    }

    avg = sum / z;

    cout << endl;
    cout << "You entered " << z << " integers";
    cout << endl << sum << " is the sum";
    cout << endl << avg << " is the average";

    cout << endl;
    system ("pause");
    }



    Now here is my problem. It works, so I'm not super worried, but it keeps saying the stack around variable x is corrupted after the program ran. I don't know why, and the teacher said he's never seen that before. Any ideas? Was I using the variables wrong or something?

    Yet again, sorry for the bad variable names

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Isn't this a Windows program?
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    85
    You have declared variable x of size 100
    and using the variable from 1 to 100, plz note index starts from 0 to 99 only
    change the index from 1-100 to 0 - 99 it should work

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    32
    I'm working in windows right now simply because I'm at school.

    Would it be different if I was in Ubuntu? I use Ubuntu at home and Java seems to be the same

  6. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    85
    In linux it works well when the memory is alloted for you. When you are accessing any location is out of your memory location. It gives you a segmentation fault.
    I think in your small program it should not create any problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •