I am using Visual Studio 2013.

Error in structure...
syntax error : missing ';' before '<<'
unexpected token(s) preceding ';'

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string"
#include "conio.h"
using namespace std;
struct input
{
    char f_name[13];
    char l_name[13];
    char gender[6];
    char section;
    int age;
    cout << "Enter Your First Name: ";
    cin.getline(f_name, 13);
    cout << "Enter Your Last Name: ";
    cin.getline(l_name, 13);
    cout << "Male/Female: ";
    cin.getline(gender, 6);
    cout << "Section: ";
    cin >> section;
    cout << "Enter Your Age: ";
    cin >> age;

};
void write_file()
{
    system("cls");


    ofstream myfile;
    myfile.open("School.txt",ios::ate);
    myfile <<"Name: Haider Akbar\n";
    myfile << "Gender: Male\n";
    myfile << "Section: B\n";
    myfile << "Age: 21\n";
    myfile.close();

}
void read_file()
{
    system("cls");
    cout << "\t\tFILE OUTPUT\n\n\n";
    string line;
    ifstream myfile("School.txt");
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            cout << line << '\n';
        }
        myfile.close();
        system("pause");
    }

    else cout << "Unable to open file";

}
int _tmain(int argc, _TCHAR* argv[])
{
    int op;//using for switch Options

    while (true)
    {
        system("cls");
        cout << "1. Write on File" << endl;
        cout << "2. Read from File" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter Selection:";
        cin >> op;
        switch (op)
        {
        case 1:
        {
                  write_file();
                  break;
        }
        case 2:
        {   

                  read_file();
                  break;
        }
        case 3:
        {
                  return 0;
        }



        default:
            break;
        }


    }//end of wile-loop
    return 0;
    system("pause");

}

Recommended Answers

All 5 Replies

I don't believe the likes of these statements cout << "Enter Your First Name: "; are allowed within a structure.

It'd be good when you post the whole error message including line-numbers etc. Otherwise finding the error is like finding the needle in a haystack.

move lines 14-23 into a method, you can't have executable code outside functions.

you should post all error message, with error lines

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.