hey guys, im fairly new to this so feel free to tell me if i need to supply more info.
basically i need to write code to read in a text file and store different values in different variables, so for example i would read in:

bob
1.1
maths
1.2
physics
1.3
chemistry
jim
2.1
dance
2.2
programming
2.3
gym


ect ect

this would bassically be for code to allocate students into classes so the names are the teachers and the subjects things they teach, with the numbers representing class codes. if all i can get to work is reading in and storing the values appropriatly that would buy me time (im pretty close to the deadline!).

thanks for your help, sorry if my question has been asked before but i didn't find a good answer on google.

bigdan

Dani AI

Generated

This implements the pattern in your sample (teacher name, then zero-or-more pairs of class-code and subject). It follows ’s high-level steps — create a class/structure, open the file, read lines and store — and turns them into concrete parsing logic you can drop into your project. The approach below treats any line that matches a “class code” pattern (digits with optional dots like 1.1) as a code; the following line is the subject. If the next non-empty line does not match the code pattern it is taken as the next teacher name (so variable numbers of classes per teacher are supported).

Python example (small, easy to adapt):

import re

def parse_file(path):
    code_re = re.compile(r'^\d+(\.\d+)*$')
    with open(path, 'r', encoding='utf-8') as f:
        lines = [L.strip() for L in f if L.strip()]
    i = 0
    teachers = {}
    while i < len(lines):
        teacher = lines[i]; i += 1
        classes = []
        while i < len(lines) and code_re.match(lines[i]):
            code = lines[i]; i += 1
            subject = lines[i] if i < len(lines) else ''
            i += 1
            classes.append((code, subject))
        teachers[teacher] = classes
    return teachers

# usage: print(parse_file("input.txt"))

C++ example (modern, readable):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <utility>

struct Teacher {
    std::string name;
    std::vector<std::pair<std::string,std::string>> classes;
};

static std::string trim(const std::string &s){
    auto b = s.find_first_not_of(" \t\r\n");
    if (b==std::string::npos) return "";
    auto e = s.find_last_not_of(" \t\r\n");
    return s.substr(b, e-b+1);
}

std::vector<Teacher> parse(std::istream &in){
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(in,line)){
        auto t = trim(line);
        if (!t.empty()) lines.push_back(t);
    }
    std::regex code_re(R"(^\d+(\.\d+)*$)");
    std::vector<Teacher> out;
    size_t i = 0;
    while (i < lines.size()){
        Teacher T; T.name = lines[i++];
        while (i < lines.size() && std::regex_match(lines[i], code_re)){
            std::string code = lines[i++];
            std::string subj = (i < lines.size()) ? lines[i++] : "";
            T.classes.emplace_back(code, subj);
        }
        out.push_back(std::move(T));
    }
    return out;
}

Notes and quick troubleshooting: strip blank lines before parsing; if subject names can start with digits the code-detection rule will misclassify — either change the file format (blank line or prefix markers) or use a stricter marker for codes. If the file is huge, parse it streaming (read teacher, then read lines one-by-one and push pairs) instead of loading all lines into memory. ’s pointer to file-IO examples is useful for learning the basic open/read loop if you need more background.

Recommended Answers

All 4 Replies

Create the class
Open the file
Read a line
Place it in the variable
continue.

Pretty basic stuff.

waltP
sorry to be a pain but what is the code to itterate through the file doing different things with each line? is it any different from doing the same thing to every line?

waltP
sorry to be a pain but what is the code to itterate through the file doing different things with each line? is it any different from doing the same thing to every line?

No, it's no different.

Sorry, but what you are actually asking us to do is teach you how to do a loop, open a file, read a file, and create and store data into your class. This is a help forum, not a teach me forum. Your instructor should have taught you all the things I mentioned. Your job is to put them together to accomplish the project. Our job is to help you when you get stuck, not teach you from scratch.

Check out link

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.