A program that reads a file.

I need to write a program that would take student names and GPA from a file and print the names of students with GPA >3.4 into another file, my problem is that I don't know how to combine files and strings together here in this example. Please someone tell me how it should be done since I'm still new to c++.
I forgot to mention that my college uses the stdio.h library and I don't understand the iostream and other stuff.
Last edited on
Hello isuckatcpp,

If your college says to use either the class is really C not C++ or you should find a better college that will teach you what you need to know. "stdio.h" is an old C header file and has no real place in a C++ program.

After reading Repeater's link you will this for using files:
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
#include <iostream>   // <--- For standard input and output. "cin", "cout"
#include <fstream>    // <--- For reading and writing  to files. 
#include <chrono>     // <--- Needed for line 17 and 22.
#include <thread>     // <--- Needed for line 17 and 22.

// ************************************ INPUT *****************************************************

std::string iFileName{ "" };  // <--- Put file name here.

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);  // <--- No need to continue.
	}

// ************************************ OUTPUT ****************************************************

	std::string oFileName{ "" };

	std::ofstream outFile;

	outFile.open(oFileName, std::ios::trunc | std::ios::ate);

	if (outFile.is_open())
	{
		std::cout << "\n File " << oFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << oFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);  // <--- No need to continue.
	}

This code may be a bit much, but I found it useful when first learning to use files. This code can be shortened and when you have figured out how you will have learned something. For the header files <chrono> and <thread> all you need to know right now is that they are needed for lines 17 and 22. And in those lines the number in the inner () is the number of seconds that it will sleep or pause.

I suggest what you work on first is to get the file or files open before you continue with the rest of the program.

Work up some code, post it along with any problems that you are having and we can go from there.

Hope that helps,

Andy
Hello isuckatcpp,

Some how I messed up the first line and did not catch it until much later. it should read:
If your college says to use "stdio.h" either the class is really C and not C++ or you should find a better college that will teach you what you need to know that is more up to date. "stdio.h" is an old C header file and has no real place in a C++ program.

Sorry.

Andy
Thank you so much guys for your amazing help and support, I'm already in love with this community, can't wait to give back to some newbies one day :). Much appreciated Mr. Andy.
Topic archived. No new replies allowed.