Beginner problems, fstream related. Class assignment due this week

Hello there, first time poster. Currently I'm in a basic intro to programming class, and I'm quickly getting lost. I can google my way through most programming assignments but this one has me frustrated and stuck. I understood perfectly how to do the example in class, but the professor rushes through explaining details succinctly and I only retain that information for as long as the code is in front of me.

I'm supposed to make a program that reads numbers one by one from a text file and decide if it is well-ordered or not (from lowest to highest) and then print out the results. My problem is in figuring out how to read numbers one by one from a file. .get is what I'm currently trying to wrangle, but it's giving me errors a lot of the time.

I can get it to read the lines of numbers, but how do I use those in an If Else?

here's my code





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
 #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    int i;
    char c1[10], c2[10], c3[10], c4[10], c5[10], c6[10], c7[10],c8[10], c9[10]; // should these be different?
    char *numbers = "numbers.txt";
    ifstream file(numbers);

    if (!file) 
	{
        cout << "There was a problem opening file "
             << numbers
             << " for reading."
             << endl;
        return 0;
    }
    cout << "Opened " << numbers << " for reading." << endl;
    file.getline(c1, 10); // footnote 1
    file.getline(c3, 10); // footnote 2
    file.getline(c4, 10);
    file.getline(c5, 10);
    file.getline(c6, 10);
    file.getline(c7, 10);
    file.getline(c8, 10);
    file.getline(c9, 10);
    
    

    return 0;
}


footnote 1: I'm not quite sure if I should use getline or get, but getline gets results while get only returns no values

footnote 2: also, I know that the 10 represents how many characters to read, but how can I write this in a variable way that it reads a certain number of characters and then tests all of them?


Last edited on
Topic archived. No new replies allowed.