0

It's been a while (Java class last year). Been trying to learn C++ on my own since my school doesn't offer it. I wrote a simple program just to test what I have learned so far - really just the syntax - before I get into intermediate stuff. Anyways I just want to highlight that I am never looking for answers, I rather you question me on my logistics so I can rethink things and possibly finish it on my own. I thought that since I can write this successfully in Java that all would be well in C++ but I am having variable issues. I tried to debug and step through but I still did not understand WHY some of my variables were not getting the values that I assigned them. If you can point me in the right direction, I would really appreciate it.

// This program will create any number of teams the user chooses, 
// give each a score and calculate the average of all the teams.

#include <iostream>
using namespace std;

int main(){

    //number of teams
    int teamCount;
    //array to keep scores
    int team[0];
    //total of scores
    int total=0;
    //average of all scores
    int average=0;

    cout<<"How many teams do you want to keep scores of?"<<endl;

    cin>>teamCount;

    //cout<<teamCount;

    //ask the person for the score as many time
    //as there are teams.
    for(int i=0; i<teamCount; i++){
        cout<< "Give me the score of team "<< i+1<<":"<<endl;
        cin>>team[i];

        total+=team[i];
    }

    average = teamCount/total;

    //output the list of the scores
     for(int i=0; i<teamCount; i++){
         cout<<"Team "<<i+1<<" score is:"<<team[0]<<endl;
     }

    cout<<"and the average of all scores is "<<average<<endl;

    return (0);

} 
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Addy75
  • 167
  • 2
  • 10
  • Please name the variables which cause problems, what value you expect them to have and what values they actually have. – Philipp Jun 04 '13 at 14:55
  • try to read up on std::vector, or i.e. http://isocpp.org/tour . C++ is not Java as you see from your experiment – Dmitry Ledentsov Jun 04 '13 at 14:55
  • make int team[0]; -> int team[100]; where a user can only input a number less than 100 and average should be total/teamcount..and then you have hard coded team[0] in the output of scores – sethi Jun 04 '13 at 14:57
  • I don't question your logistics cause I don't know what you transport :) Your logic fails, when you think you can just grow your array 'magically'. Check std::vector. – Vaaksiainen Jun 04 '13 at 14:57
  • someone has answered the question, but in general the `I just want to highlight that I am never looking for answers` means that this doesn't really belong here (this is supposed to be Q+A). – Shep Jun 04 '13 at 14:57
  • 1
    Disappointed to hear your school doesn't offer a C++ course! – Tom Jun 04 '13 at 14:58
  • @ShafikYaghmour: Code Review is not for troubleshooting errors, it is for making improvements to code that is already working (or at least, appearing to work) correctly. – Benjamin Lindley Jun 04 '13 at 14:59
  • Thank you. I do remember the tuts saying that arrays are not dynamic. I am indeed confusing it with Java. – Addy75 Jun 04 '13 at 15:05

4 Answers4

6

Your array

int team[0];

will not work in C++. Btw you can't allocate 0-sized array this way
Try c++ containers instead

std::vector<int> team;
spiritwolfform
  • 2,263
  • 15
  • 16
3

Your team array has no storage associated with it. in C++ arrays are not dynamic, try using a vector instead, and resize it when you read teamCount

Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40
  • 2
    Well, `int team[0];` is actually zero `int`s long. `team` is a distinct, non-`NULL` pointer, but dereferencing it is UB. – Joker_vD Jun 04 '13 at 14:57
  • team[0] isn't one int long. – Andrew W Jun 04 '13 at 14:59
  • Yup. Arrays not being dynamic makes sense. I am going to review the tut on vectors and redo the code. (I wondered why the tutorial got into vectors before arrays - now I see). – Addy75 Jun 04 '13 at 15:09
2

In the line

int team[0];

you are creating an array with 0 entries. Arrays in C++ can not increase or shrink. To solve this issue, either allocate the array dynamically after you know how large it needs to be:

int * team = new int[teamCount];

(don't forget to call delete[] team; when you don't need it anymore, or the memory is never reclaimed)

Or better use the object-oriented way and use the class std::vector which is the C++ equivalent for the Java class ArrayList.

Your next mistake is here:

//output the list of the scores
 for(int i=0; i<teamCount; i++){
     cout<<"Team "<<i+1<<" score is:"<<team[0]<<endl;
 }

You are outputting the value of the first team again and again during each loop iteration.

By the way: Both mistakes would be just as wrong in Java :)

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Was that downvote a tactical one or is there something wrong with my explanation? – Philipp Jun 04 '13 at 15:08
  • (I don't know what a downvote is.) You're explanation is great. Everyone reminded that vectors are not dynamic. That seems to be the start of my issue. And I just learned about "garbage" yesterday so thanks for reminding me about that so I can implement that also in my code! – Addy75 Jun 04 '13 at 15:13
  • The downvote went for mentioning `new[]` before `vector` (or rather, for mentioning `new[]` at all). – Griwes Jun 04 '13 at 15:21
  • oh crap. That was supposed to be team[i] - now that one I'm just ashamed about. That was mental. – Addy75 Jun 04 '13 at 15:21
  • @Griwes In the future I would urge you to only downvote answers when they are objectively wrong or lack information, not just because you subjectively disagree with them. The new[] operator for arrays is a language feature, whether you like it or not, and I pointed out that using a vector is the better way. – Philipp Jun 04 '13 at 15:25
  • 3
    @Griwes: You downvoted him for providing more information? That is objectively a very stupid reason to downvote. – Benjamin Lindley Jun 04 '13 at 15:30
  • @BenjaminLindley, I downvoted him for providing information that is counter-useful nowadays. – Griwes Jun 04 '13 at 15:31
  • @Philipp, you did mention it. But, if it is better, why even mention `new[]`? Why put a code example with `new[]` instead of `vector` in the post? I assume you do know how it works - people often read just the code, not the explanation. Or the code and few words before and after. – Griwes Jun 04 '13 at 15:32
  • There is a serious mistake: You have to call `delete[] team`! – MWid Jun 04 '13 at 15:34
  • Question Philip, I see you put "int * team = new int[teamCount];" with the reminder to delete[] team. I went back and read up the stack, the heap and garage so the statement makes sense to me now. But my question to you is why? (I'm learning not suggesting). When I received my teamCount from the user, couldn't I have just declared "int team[teamCount]" after the cin statement and all would have been well? Why the pointer? Why the "new" keyword when my array size isn't dynamic anymore after my cin statement? Why is it better to do it the way you described? – Addy75 Jun 06 '13 at 00:45
  • @Addy75 short answer: Because the C standard says that array sizes need to be known at compile time :) - the long answer would warrant a new question. – Philipp Jun 06 '13 at 12:55
  • @Philipp thank you. That was more than enough. You gave me a starting point for my research. Thanks again! – Addy75 Jun 06 '13 at 13:27
1

Try this:

average = total / teamCount; //Lets calculate the average correctly. Note: Integer division

//output the list of the scores
 for(int i=0; i<teamCount; i++){
     cout<<"Team "<<i+1<<" score is:"<<team[i]<<endl; //We want each value, not only team[0]
 }
VoidStar
  • 936
  • 1
  • 7
  • 17