Sign in to follow this  

Problems with code

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

If you intended to correct an error in the post then please contact us.

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 << "Shadow gate - A massive rpg adventure." << endl;
  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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jakpandora
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!


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)

88 shadow gate csource.cpp
excess elements in aggregate initializer

any help?

Share this post


Link to post
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 this post


Link to post
Share on other sites

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;



You know how to use cin, but you're not using it for the name. Aaargh.


string name;
...
cin >> name;
// won't crash if the user types more than 34 letters
// also allows us to get rid of that ugly, ugly stdio.h library
// and use C++ like Stroustrup intended.
// If you want to limit the name length, you can trim it down
// at this point in a variety of ways, or loop until the user
// inputs a name you like.
// If you want to allow spaces in a name, you can still do that
// easily with the new-style tools: look up std::getline().
cout << "I see great potential for you, " << name << " the warrior." << endl;
// works exactly the same as before. Plus you will benefit from
// all the other nice things that std::string offers you, but
// most importantly, safety!

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this