Public Group

# Problems with code

This topic is 5088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I got 30 erros when compileing this code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;

int main(int argc, char *argv[])
{
//Declare local variables

int playerh; //Player health
int playerg; //The amount of gold the player has
int players; //The amount of stanima the player has
char yn; //choice of yes or no for questions
char y = 'y'; //Choice of yes
char n = 'n'; //Choice of no
char d; //Direction input

//Declare arrays

char name[35]; //Player name
char town[15][4] = {    //Array for town
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
{16,17,18,19,20,21,22,23,24,25,26,27};
{28,29,40,41,42,43,44,45,46,47,48,49};
{50,51,52,53,54,55,56,57,58,59,60,61};
};

//Main code

cout << endl;
cout << "  *      *     " << endl;
cout << "  *      *     " << endl;
cout << "  *      *     " << endl;
cout << "  *      *     " << endl;
cout << "  *      *     " << endl;
cout << "  *      *     " << endl;
cout << "*****  *****  " << endl;
cout << "  *      *     " << endl;
cout << "  *      *    " << endl;
cout << endl;
cout << "What be thy name, warrior?" << endl;
gets(name); //Read the whole array. player types in name
cout << "I see great potential for you, " << name << " the warrior." << endl;
cout << "Welcome to Grant Hill village " << name << "." << endl;
cout << "Do you have any questions?" << endl;
cin >> yn;
if (yn == y)
{
cout << "I am sure you have many. However, I am not the one to answer them. You should ask people around town. They will know more then I do." << endl;
}
if (yn == n)
{
cout << "Good. We do not have time for that. If you DO have questions though, you should ask around town." << endl;
}

system("pause");
return 0;
}


I really dont feel like going and listing all thirty errors, so I would appreciate it if you just looked through my vode and told me whats wrong. thanks!

##### Share on other sites

1. #include <stdio.h> - Remember, if you're using .h with something standard like that, you can probably stick a c in front. That should be #include <cstdio>.

2. This probably isn't generating an error, but it's not good practice.

[Edited by - Ra on September 13, 2004 5:16:06 PM]

##### Share on other sites
Quote:
 Original post by jakpandoraI really dont feel like going and listing all thirty errors, so I would appreciate it if you just looked through my vode and told me whats wrong. thanks!

copy/paste not working for you? i'll be 10000000 times easier for us to help if you spend 5 seconds and copy/paste

-me

##### Share on other sites
Ok jakpandora, you've got to give people all the help you can. If you've got errors, cut n paste them, and people can help you straight off.

But, because I'm so damn nice, I compiled your code and had a look. First off, don't be terrified by thirty-odd mistakes. Some of those will be the result of the same typo, and they'll all be solved together. In this case, a tiny cock-up in your array is the source of most of your woe. Use commas to separate the rows of your array, rather than semicolons.
char town[15][4] = {    //Array for town           {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},           {16,17,18,19,20,21,22,23,24,25,26,27},           {28,29,40,41,42,43,44,45,46,47,48,49},           {50,51,52,53,54,55,56,57,58,59,60,61},         };

##### Share on other sites
And note that a 15x4 array should be 1-15,16-30,31-45,46-60. right now there's not 15 elements in parts 2,3 and 4. That will likely not do what you expect.

##### Share on other sites
I changed the array to the following at it works:

  char town[15][4] = {    //Array for town           (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),           (16,17,18,19,20,21,22,23,24,25,26,27),           (28,29,40,41,42,43,44,45,46,47,48,49),           (50,51,52,53,54,55,56,57,58,59,60,61),         };

##### Share on other sites
jeez, I don't even know where to start.

That array should be declared as town[4][15], not town[15][4]

The reason AlekM's version works is because he used parentheses instead of semicolons, so stuff inside (with all those commas) was actually *evaluated*. In C++, (1,2) evaluates to 2 and
char town[15][4] = {    //Array for town           (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),           (16,17,18,19,20,21,22,23,24,25,26,27),           (28,29,40,41,42,43,44,45,46,47,48,49),           (50,51,52,53,54,55,56,57,58,59,60,61),         };

is the same as
char town[15][4] = {15, 27, 49, 61};

also, what happened to the thirties?

and how about doing that programmatically?
for (int x = 0; x < 4; x++)  for (int y = 0; y < 15; y++)    town[x][y] = x * 15 + y + 1;

##### Share on other sites
sorry to make you so mad. I myslef was so mad the code didnt compile that I didnt even think of that. thankd for the help. also, I dont understnad what pinacolada and telesytn said about the array. could someone explain this to me?

*EDIT* just compiled the modified array code, and I get the following error four times on line 88 (the last line of the array)

excess elements in aggregate initializer

any help?

##### Share on other sites
I didn't look at your code too much, but here's what I can tell you:

You declare an int array like so:

int array_name[ ROWS ][ COLS ];

Someone else already mentioned that yours was inverted - you mean to make a 4x15, but instead made a 15x4 array.

Second, think... you have 4 rows and fifteen columns - that means you have 4x15 elements, which == 60 elements. Yours has 51. Basically, that's all the compiler is complaining about (I think)...

##### Share on other sites
thanks vodka, it worked!

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 10
• 13
• 21
• 13
• 20
• ### Forum Statistics

• Total Topics
632195
• Total Posts
3004704

×