-1

I have this programming project on file i/o in c++. I have read up about it but don't understand much of it yet. Regardless, I made this program that allows the user to store the profile ( name, ethnicity etc. ) of a person onto a text file and be able to retrieve it. However, i am encountering some issues with how it works.

what it does is at the start it asks the user if they want to view the previously created file or create a new one.

if view a previous file was selected the program will then spit out the whole text file

if creating a new file is selected the program will ask for the number of people then proceed to ask for the name, age, gender, species, ethnicity and mode of transport for the allocated number of people and save it onto the text file.

the problem is i am unable to set the number of people as the program ignores the variable (i.e. i set 5 people and it ignores it) and the program ignores the first name entered by skipping it.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char names[100];
    char age[100];
    char gender[100];
    char species[100];
    char ethnicity[100];
    char transport[100];
    char decision[0];

    string getcontent;

   cout << "Would You Like to Open The Previous File Or View Your Profiles?" << endl << endl;
   cout << "Enter 1 For Previous File" << endl;
   cout << "Enter Anything Else To View Your Profiles:  " << decision;
   cin.getline(decision,5);
   cout << endl << "==============================================================" << endl;
   if(decision[0] == '1')
   {
       ifstream infile;
       infile.open("File of names.txt");
       while(! infile.eof())//eof stand for end of file
        {
            getline(infile, getcontent); //getline requires a string
            cout << getcontent << endl;
        }
        cout << "==============================================================" << endl;
        infile.close(); //closes the opened file - good practice

   }
   else
   {
       int a;
       cout << "Enter The Amount Of People You Would Like To Store: ";
       cin >> a;
       ofstream namefile;
       namefile.open("File of names.txt");
       cout << "Please Set Your Team Profile." << endl;
       for (int i=0; i<a; i++)
        {
            cout << "==============================================================" << endl;
            cout << "Enter Student " << i+1 << " : ";
            cin.getline(names,100);
            namefile << names << endl;
            cout << "==============================================================" << endl;

            cout << "Enter The Age: ";
            cin.getline(age,100);
            namefile << age << endl;

            cout << "Enter The Gender: ";
            cin.getline(gender,100);
            namefile << gender << endl;

            cout << "Enter The Species: ";
            cin.getline(species,100);
            namefile << species << endl;

            cout << "Enter The Ethnicity: ";
            cin.getline(ethnicity,100);
            namefile << ethnicity << endl;

            cout << "What Is The Mode Of Transport: ";
            cin.getline(transport,100);
            namefile << transport << endl << endl;
        }
namefile.close();


   }

}

This is the output of the file:

Would You Like to Open The Previous File Or View Your Profiles?

Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g

==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1:==============================================================
Enter The Age:

This is the expected output:

Would You Like to Open The Previous File Or View Your Profiles?

Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g

==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1: John
==============================================================
Enter The Age:
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 3
    Related/duplicate: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Yksisarvinen Aug 06 '19 at 08:56

1 Answers1

0

The problem is that for example when you input a value like: string str; cin>>str; and you insert "John", you are not assign "John" to str, but "John\n" (note \n, that is created when you type enter on the keyboard). A possible solution to ignore it, is using cin.ignore(); However probably your homework is to make a very simple "database", so you will have to memorize the data in an orderly way, then i will suggest you to use "struct" (it's easy, not difficult for beginners).

Riccardo
  • 192
  • 2
  • 2
  • 11