0

I have no idea what I'm doing the most of the time, I try and explain it.

I am suppose to make a social program that is basically twitter but in a console application for C++. It is suppose to write/save a user's message if they choose choice 1 and read from a file provided if they choose choice 3. There are several errors in this code and I have no idea how to fix it. I am mostly new to C++ in general. Can you please help me? (Ignore choice 2, I eventually fix that myself)

  #include <stdio.h>
#include <string.h>

void printMessage(struct message_data M)
{
    int msgLeng = strlen(M.message);
    printf("ID - %s : MsgLength - %d : Upvotes - %d\n%s\n", M.userid, msgLeng, M.upvotes, M.message);
}

struct message_data
{
    char userid[11];
    char message[142];
    int upvotes;
}

mainpage(void)
{
    struct message_data message1;
    FILE *CAWCAW; // File Pointer


    int menuchoice;
    // Show menu choices
    printf("Welcome to CawCaw! Please don't sue us!\n");
    printf("Welcome to the main menu of Twitt- I meant CawCaw!\n");
    printf("Type in 1 for new CAWS\n");
    printf("Type in 2 for viewing CAWSCAWS\n");
    printf("Type in 3 for loading a CAWCAW from a file\n");
    //printf("Type in 4 to quit CAWCAW\n"); (NOT NEED FOR NOW, JT 30/11/15)

    //Menu choice
    scanf("%d", &menuchoice);
    //Case statements
    switch (menuchoice)
    {
        case 1: //Adding a new caw
        {
            printf("WHAT'S YOUR CAW NAME? \n"); 
            fscanf(CAWCAW, " %[^\n]", &message1.userid);
            printf("WHAT'S YOUR CAW STRANGER? \n");
            fscanf(CAWCAW, " %[^\n]", &message1.message);
            printf("THANK YOU STRANGER, YOUR CAW HAS BEEN UPLOADED \n");
            //^^^ Asks users for details , such as name messages, whatever
        }
        break;
        case 2: // Viewing cawcaws
        {

        }
        break;
        case 3: //Loading a cawcaw from a file
        {
            CAWCAW = fopen("D:/FILEDIRECTORY/FOLDER", "r"); //PLEASE CHANGE DIRECTORY TO THE CORRECT ONE WHEN LOADED FOR REAL!
            char userid[11];
            char message[142];
            int upvotes;
            while (!feof(CAWCAW))
            {
                fscanf(CAWCAW, "%10[^|]|%141[^|]|%d", &userid, &message, &upvotes);
                printf("%s, %d, %d\n", userid, message, upvotes);
            }
            fclose(CAWCAW);
        }
        break;
        default:
        {
            printf("PLEASE PUT IN A VALID CHOICE NUMBER, CAW CAW! \n");
        }
    }
}

The new errors I am getting are this:

Severity    Code    Description Project File    Line
Error   C2027   use of undefined type 'message_data'    NOTTWITTERCAWCAW        6
Error   C2228   left of '.message' must have class/struct/union NOTTWITTERCAWCAW        6
Error   C2027   use of undefined type 'message_data'    NOTTWITTERCAWCAW        7
Error   C2228   left of '.userid' must have class/struct/union  NOTTWITTERCAWCAW        7
Error   C2228   left of '.upvotes' must have class/struct/union NOTTWITTERCAWCAW        7
Error   C2228   left of '.message' must have class/struct/union NOTTWITTERCAWCAW        7
James.T
  • 31
  • 5
  • Can you edit your question to include the errors? It's a bit hard to guess what's up otherwise. – GrandMasterFlush Nov 30 '15 at 23:40
  • Alright then added the errors as well thank you – James.T Nov 30 '15 at 23:49
  • 1
    You should search StackOverflow for similar assignments: "c++ twitter read file" – Thomas Matthews Nov 30 '15 at 23:55
  • 3
    Since this is C++ don't use `struct` when declaring variables. – Thomas Matthews Nov 30 '15 at 23:56
  • 3
    define `message` before you use it, how is `void printMessage(struct message M)` supposed to know what a `message` is if it hasn't seen its definition? `mainpage(void)` should be `void mainpage()`. What is `false(CAWCAW);` supposed to do? [`while (!feof(CAWCAW)`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) completes the C++ error bingo. – user657267 Nov 30 '15 at 23:57
  • In your `message` structure, you have a variable with the same name as the class. This is generally not a good idea, I suggest you change it to some thing like "message_data". – Thomas Matthews Nov 30 '15 at 23:57
  • 4
    Since you are using C++, I **strongly** recommend using `std::string` rather than character arrays. – Thomas Matthews Nov 30 '15 at 23:58
  • Hello sorry for the long reply. I have done some suggestions such as changing False(cawcaw) to fclose. (Ty user657267). Unfortunately some suggestions such as using std::string than character arrays I cannot do because I haven't been taught it and I really have no idea how to define messages etc. I'm bad at this sorry. – James.T Dec 01 '15 at 00:21
  • you're missing a semicolon at the end of your struct declaration. – Adam Dec 01 '15 at 00:35
  • also why did you mark this as C++ when you insist on doing everything the C way? – Adam Dec 01 '15 at 00:46

1 Answers1

0

you main error is that you're defining the struct after you use it in the printMessage() Method. Just define the struct before this method should clear up you'r errors.

Your program seems more like a C-Programm rather than a c++ Prog., because you could do this a bit easier than with a file-pointer and char*. Maybe look into C++ Read/Write Files.

MrNice
  • 646
  • 8
  • 23