Hi, this is my first time posting here so if I violated any rules I am truly sorry.
I am a new CS student and I've been learning programming in C++ for almost 13 weeks, bare with me because I don't have much experience in anything and my codes are very very basic. So what I am trying to do right now is I have a file, and I know how to ifstream read file but how do I let the user input and filter out the numbers/subjects he wants to see from the file?
Example scenario:
I have a class of students each with different name, student IDs, studying in different grades, and have the same 7 subjects.
So I let the user input the grades and subjects that he wants to see. Then it will read the file and cout the matching students. So how do I do that?
Thanks in advance, hope to see simple solution since I have not learn about vector or anything. I have only learnt about function, iteration, input and output, arrays, strings and struct. I am using VS 2019 and programming in C++ language.
What if I want the loop to cout the found statement? I tried replacing the cout inside the if loop with cout<<findgrade<<": "<<grade<<endl; but it doesnt cout anything.
Here's my code.
Thanks for correcting my careless mistake! But after these adjustments it still wouldn't cout anything after "Filtered list:" . Here's a sample of the text file.
21XXX0213
Adrian Choo
89 82 44 56 99 94 70
21ACV0231
Jenny Lee
67 90 66 54 98 74 81
etc.
The text file is stored in the same folder as the .cpp file. There's also no extra spacing after each line of text so I am wondering why it still wouldn't cout anything after the adjustments.
Also, I'm wondering if using function would make the codes easier to understand and shorter, I have the idea in my head but I couldn't make it work, any suggestions?
Thanks for your help in advance! I really do appreciate the replies and it helps me a lot in class!
if it does not print anything after filtered list, odds are, it failed to open that file.
you can prove this to yourself in the debugger or with some additional print statements, then try to figure out what is going on there.
some IDEs (visual specifically) change the working path behind your back, so a file that you think it would see may not be in the 'current folder' when it runs. On unix, the file name is case sensitive. There could be some other cause too, but those look likely.
Thanks for the response! Currently I've tried solving the issue , and made some improvements. Now it does cout the statement but it still doesn't cout the marks for the subjects. And also if the user input multiple subjects in 1 input , how do I make sure the code filter also works in that way?
So below is the desired output. I don't know how to achieve it.
Please select your choice:
1. Filter
2. Search
Choice: 1
Please key in the information that you want to filter.
Form: 1
Subject(s): BC Sci
Filtered list:
Student ID: 20ACD1234
Student Name: Alice Lim
Form: 1
BC: 78
Sci: 86
Student ID: 20ACD1235
Student Name: Derrick Tan
Form: 1
BC: 71
Sci: 68
Inside the text file the writing format is like this
Student ID
Student Name
Grade(form)
BM BI BC Maths Sci Sejarah Geo
What I am having trouble right now is
1. The subject filter cannot cout anything
2. How do I make the code able to filter 2 subjects in 1 input ? (Subject(s): BC Sci)
Hi! Sorry for the late reply! I've been working on the code myself and I kind of succeeded on what I originally planned to do. In the end I basically scratched the whole code and did it from scratch again, which included struct and function parameters and definition . It made the whole code more understandable and it still works . And I decided to limit the user to only enter 1 subject to search instead of being able to enter multiple subject to search which would require to store the user input inside an array , I don't have the brains to do that lol.