Array Tutorial Doesnt Work

This is a discussion on Array Tutorial Doesnt Work within the C++ Programming forums, part of the General Programming Boards category; I am learning C++ through the book C++ HOW TO PROGRAM by Deitel & Dietel. I tried an array program ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Array Tutorial Doesnt Work

    I am learning C++ through the book C++ HOW TO PROGRAM by Deitel & Dietel. I tried an array program that is supposed to count the frequency of one array by using a second, but the program doesnt work when I try to compile it. It returns a value of 0 for all subvalues of array Frequency[]. I am using Visual C++ 6.0 on a PC. heres the program:

    ////////////////////////////////////////////////////////////////
    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    #include <iomanip>

    using std::setw;

    int main()
    {

    int responses[40]={1,2,6,4,8,5,9,7,8,10,1,6,
    3,8,6,10,3,8,2,7,6,5,7,6,8,6,7,5,6,6,5,6
    ,7,5,6,4,8,6,8,10};

    int frequency[11]={0};

    for (int answer=0;answer<40;answer++);
    ***--------->*** ++frequency[ responses[ answer ] ];

    cout<<"Rating"<<setw(17)<<"Frequency"<<endl;

    cout<<frequency[5]<<endl;

    for (int rating=1;rating<11;rating++)
    cout<<setw(6)<<rating
    <<setw(17)<<frequency[rating]<<endl;
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////

    The debug shows the problem to be with the starred line, and im assuming it is because of the ++ but i dont know how to add 1 to that array value using a different method. Please help!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >for (int answer=0;answer<40;answer++);

    See that semicolon at the end of this line? Because of that, the increment is outside the for-loop. Omit the semicolon and see if that helps.

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    I don't know if this will solve it but I'm seeing two errors:

    1. The compiler says rating is undeclared here:
    Code:
     for (int rating=1;rating<11;rating++)
    cout<<setw(6)<<rating
    <<setw(17)<<frequency[rating]<<endl;
    return 0;
    Because there are no braces around the statements, that looks like a mess.

    2.
    Code:
     for (int answer=0;answer<40;answer++);
    shouldn't have a semicolon after it.
    Last edited by Padawan; 04-06-2004 at 06:52 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Kill the semi-colon on the for loop
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    oh, hehehe.... sorry about not having tags either! thanks for helping the noob.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i get array work for string?
    By skydancer in forum C Programming
    Replies: 10
    Last Post: 07-31-2006, 11:18 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. assigning 2d array to type **arr doesn't work
    By scoobasean in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2005, 02:14 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21