-4

I'm having trouble using structs to program. I want to accept input in the form of (integer, character) which can repeat several times. The program will then store the character in the array in the spot where the integer indicates. Currently, the issue is that Message is undefined and that location is undefined.

struct MessagePiece
{
    int location;
    char message;
};

void readMessage( istream& in, Message message[] )
{
    MessagePiece;
    message[256];
    Message message;

      while ( !in.fail() )
     {
             in >> location; //I'm not sure why this counts as undefined as it is defined in the struct

             if (location < 256, location >= 0)
                in >> message[location];
      }
return;
};
  • 1
    There are some pretty basic flaws in your code. It may be more productive to start by reading [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Mar 21 '13 at 21:59
  • 4
    Welcome to Stack Overflow. While you have told us about your problem, you haven't shared with a question. Stack Overflow is a **question**-and-**answer** site. Do you have a specific question? – Robᵩ Mar 21 '13 at 21:59
  • 1
    You are in the serious need to read up on the ***very basics*** of C++. This is a ***very bad question*** you just posted. –  Mar 21 '13 at 21:59
  • http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jerry Coffin Mar 21 '13 at 22:07
  • You are using the comma operator in your `if` statement. It should be `if ((location < 256) && (location >= 0))`. – Thomas Matthews Mar 22 '13 at 00:06

2 Answers2

2

It is undefined because it the location exists only in the context of an object of type MessagePiece

MessagePiece mp;
in >> mp.location;
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Thank you for your help instead of insulting my ability to code. – Stop NOW Mar 21 '13 at 22:01
  • Also not to mention he is using the variable message in several spots, which will throw an error – Kyle C Mar 21 '13 at 22:02
  • Don't forget about his if statement which only checks one condition. – David G Mar 21 '13 at 22:03
  • 5
    @StopNOW: You're welcome, but seriously your code has about a dozen other issues and I really agree with people who refer you to basic C++ books. You can't learn C++ with online tutorials. Do refer to a book – Armen Tsirunyan Mar 21 '13 at 22:08
0

You should use && instead of ',' in your if statement for checking the condition. Also, these lines:

MessagePiece;
message[256];
Message message;

should be written like this:

MessagePiece messages[256]; //declaring an array of struct MessagePiece
char message; // a char for storing input read from the user. 

One more thing, either take message as an argument or declare it in the function itself.

Please consider reading a good book on programming in C++.

A human being
  • 1,220
  • 8
  • 18
  • But `message` is also the name of one of the function's parameters. I think the code would benefit from some more informative names (among other things). – Keith Thompson Mar 22 '13 at 00:31
  • yes, that's why I said that either declare it in the function or pass it as an argument, but not both – A human being Mar 22 '13 at 10:47