closes program after first set of instructions

it ends after outputting the names after first import


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  string usersFile; //stores users file name
  const int MAX_RNDNAMES = 5; //list size
  int nrndNames = 0; //list capcacity
  string rndName[MAX_RNDNAMES]; //name array
  int u;
  string tempVar;

  //user introduction
  introduction(objective, instructions);

  // get user file name
  getline(cin, usersFile);
  cout <<endl;

  // open input file
  ifstream fin; //
  fin.open(usersFile.c_str()); //
  if (!fin.good()) throw "I/O error"; //
  
  // getnames
  do
  {
    getline(fin, rndName[nrndNames]);
    cout << rndName[nrndNames] <<endl;
    nrndNames++;
  }while (fin.good());
  
  //close input file
  fin.close();
  
  // open output file
  ofstream fout; //reserve "fout" for use
  fout.open ("friends.txt"); //open specifed file
  if (!fout.good()) throw "I/O error"; //if file isnt open output error to console

  //sort
  for (int j = 0; j < nrndNames; j++)
  {
    if (rndName[j] > rndName[j+1])
    {
      tempVar = rndName[j]; 
      rndName[j] = rndName[j+1];
      rndName[j+1] = tempVar;
    }
  }

  //output
  for (u = 0; u < nrndNames; u++)
  {
    cout << rndName[u] <<endl;
    cout << endl;
    fout << rndName[u] <<endl;
    fout << endl;
  }

  //close output file
  fout.close();

}//main 
How many names are in the file ?
it ends after outputting the names after first import

Yes it does. Do you have a question about that? You've said why you have a question, but not what your question is. Is it supposed to loop and the code isn't working? Are you asking how to make it loop?

It looks like you're trying to bubble sort the data at lines 37-45, but you only have one loop. Bubble sort requires an inner loop and an outer one.
Hello pnwadike99,

Although Thomas1965 makes a good question it is also irrelevant at this point because the code that you have provided does not compile. You are missing the include files, "main" and it looks like a function.

I did make some adjustments to get the program to read a file.

I have no idea what IDE/compiler or what C++ standards you are compiling to because you did not mention this.

The code below was compiled with the 2014 standards, but should work just fine with the 2011 standards.

1
2
3
4
5
6
7
8
9
10
int main()
{
    constexpr int MAX_RNDNAMES{ 5 };  // list size
    const std::string PATH{ "F:/Text Files/" };

    int nrndNames{}; //list capcacity
    int count{};  // <--- Or you could use "size". Keeps a count of how many reads happened.
    std::string usersFile{PATH + "5 Random Names.txt"}; //stores users file name
    std::string rndName[MAX_RNDNAMES]; //name array
    std::string tempVar;

I left line 4 as an example. One to show that the forward "/" works as well as the backslash "\". The other is to show how line 8 can use this to create a full string to a file name. This can be used later when opening the file stream.

Other variables other than "std::strings", "std::vector" and other containers should ALWAYS be initialized. If for no other reason than to know that they do not contain garbage values. The empty {}s, available from C++11 on, is the same as saying "= 0".

introduction(objective, instructions);. This appears to be a function call, but there is no function in the code and the 2 parameters are never defined.

1
2
// get user file name
getline(cin, usersFile);

This is nice, but does the program come with a copy of the source code so that the user can try and figure out what to enter instead of staring at a blank screen having no idea what to do. It is best to provide a prompt when asking for input.

1
2
3
// get user file name
std::cout << "\n Enter file name: ";
std::getline(std::cin, usersFile);

Notice that the string ends with ": " and no "\n" or "endl". This puts the "cin" on the same line as the prompt.

The prompt can be changed to anything that you like.

Your code:
1
2
3
4
5
6
// open input file
ifstream fin; //
fin.open(usersFile.c_str()); //

if (!fin.good())
    throw "I/O error"; // 

In three lines of code you started a comment, but never finished it. If you do not have a comment then you do not need the (//) at the end of the line.

From C++11 on you do not need the ".c_str()" to open a file stream. A std::string will do.

For now your if statement is better written this way and leave the 1 line for later. This will help you understand the code better until you get use to it.

A simpler way to writhe code is:
1
2
3
4
5
6
7
8
std::ifstream fin(usersFile);

if (!fin)
{
    std::cerr << "\n     File \"" << usersFile << "\" did not open!\n ";

    return 1;
}

I used the if statement this way because the "throw" part was giving me problems.

I am not familiar with using "throw", but I believe that it needs to be in a try/catch to work properly. Your way may set a "throw", but it does not stop the program, so you continue as if nothing is wrong.

You do have the opportunity to loop until a valid file name is entered.

When reading the file your do/while loop may work, but the more used method is
1
2
3
4
5
// getnames
while (count < MAX_RNDNAMES && std::getline(fin, rndName[count]))
{
    std::cout << rndName[count++] << '\n';
}

The first part of the while condition is to make sure that you do not go past the end of the array. The second part will read the file until it sets the "eof" bit and caused the condition to fail, but the first part would likely fail first unless the array was bigger than needed to start with.

When opening the output file stream refer back to what I did for the "ifstream".

Here the if statement is not needed because if the output file does not exist it will be created before it is used, so it is less likely to fail. Now if you have a path to the file then you need the if statement because the path may be a problem that you will need to tap.

Your sort code, which I nave not worked on at all, does not work.
First j < nrndNames will try to sort something past the end of the array when you get to rndName[j + 1]. This is most often done with nested for loops.

Not sure exactly why, but the output section at the end of "main" did not work in the beginning. I do not know if the changes I made to the for loop made the difference or if was something else. That part of the program I was not watching at first. I may have been because the file was not being read and it was printing blank lines, which is hard to tell when that happens.

I would add to this, try and use the "endl"s sparingly if you can as they cause overhead that could slow the program down.

Andy
Topic archived. No new replies allowed.