Need Help running program from cmd prompt

Hi, I just needed help running a program from the cmd prompt as was required by my prof for an assignment question. It basically needs to run after typing a3 file_name in the cmd prompt. a3 is the name of the executable while file_name is the text file. I dont know understand what else to do besides writing those parameters in the main. The following is the question:

Write a program called a3 that can be run from the command line prompt
using the following syntax: a3 file_name, where a3 is the name of the .exe file,
file_name is a text file containing a list of student numbers with their corresponding GPA
separated by a space; one student per line. The text file can contain any number of lines. An
example of file_name containing 3 lines is as follows:
100123456 3.7
100654321 4.0
100246800 2.8
After reading the contents of the file, the program outputs the highest GPA from the list. For
example, from the students in the sample above, the output of your program should be
something like “Highest GPA was 4.0, by student 100654321.” Submit all the source code
and executable (.exe) 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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


int main(int argc, char* argv[])

{
	ifstream readFile;
	//ofstream writeFile;

	readFile.open("file_name.txt");
	//writeFile.open("file_name2.txt");
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	readFile.precision();
	
	int firstStudent; double firstGPA; int secondStudent; double secondGPA; int thirdStudent; double thirdGPA;
	readFile >> firstStudent >> firstGPA;
	readFile >> secondStudent >> secondGPA;
	readFile >> thirdStudent >> thirdGPA;
	
	cout << firstStudent << setw(6) << setprecision(1) << firstGPA << endl << secondStudent << setw(6) << secondGPA << endl << thirdStudent << setw(6) << thirdGPA << endl;
	
	if (firstGPA > secondGPA && firstGPA > thirdGPA)
	{
		cout << "Highest GPA was " << firstGPA << ", by student " << firstStudent << endl;
	}
	else if (secondGPA > firstGPA && secondGPA > thirdGPA)
	{
		cout << "Highest GPA was " << secondGPA << ", by student " << secondStudent << endl;
	}
	else
		cout << "Highest GPA was " << thirdGPA << ", by student " << thirdStudent << endl;
	
	readFile.close();
	//writeFile.close();

	return 0;
}
Last edited on
A few problems with your program:

1) The instructions say to accept the filename from the command line. You can find the first argument (filename) in argv[1].

Line 13 should be:
 
readFile.open (argv[1]);


2) The instructions don't say anything about opening an output file. The instructions say "the program outputs", the implication is to cout. You actually never do anything with writeFile other than to open and close it.

3) lines 21-23. The instructions say "The text file can contain any number of lines.". You only allow three lines. You need a loop that continues until an input operation fails.

4) Lines 27-36: This logic won't work with more than three lines.







Find your executable in explorer, then click on the address bar at the top and type "cmd" and press Enter. That will open a DOS command prompt.

Type the name of your program and the name of the text file (which should also be in the same folder):

    C:\Users\Ravshan\Documents\Schoolwork\Assignment12> a3 filename_text2.txt  

You will see the output.

Your program currently ignores its arguments. Fix it thus:

7
8
9
int main(int argc, char* argv[])
{
	ifstream readFile(argv[1]);

It is also a good idea to see if the file was able to be opened.
11
12
13
14
15
	if (!readFile)
	{
		cout << "Could not open file " << argv[1] << "\n";
		return 1;
	}

Hope this helps.
Hi, currently, should the executable file show up in the explorer cause currently all that shows up is the following:

Debug
a3.cpp
a3.vcxproj
a3.vcxproj.filters

After typing in cmd, i get this:
C:\Users\100584220\Documents\Visual Studio 2013\Projects\a3\a3>
so i type in a3 file_name.txt after it doesnt work, instead says a3 is not recognized.

Is there anything im doing wrong? Thanks.
You need to find a3.exe in explorer, not the a3.cpp file. Since you are compiling in Debug mode, look down somewhere in the Debug folder.

Don't forget: your "file_name.txt" file should also be in the same folder as a3.exe.

Thanks i got that figured out but should it work by just typing a3 file_name or does it have to have .txt as in a3 file_name.txt for it to work cause currently it doesnt work with just a3 file_name alone.

Also, could someone please help me figure out how i can make the program so it can take in any number of lines, im confused on how it will read in the student number and then a space and then read in the gpa. Any help is appreciated. Thanks,
You cannot open a file unless you have its exact file name, so yes, you must say "a3 file_name.txt". If you want, you can do things like check for an extension if opening the file fails, and try again with the argument filename + ".txt".

In order to process any number of students, you need a vector (or array) of structs, where each struct represents one student.

1
2
3
4
5
struct Student
{
  int ID;
  double GPA;
};
(using vectors)
1
2
3
4
5
6
7
  std::vector <Student> students;

  Student student;
  while (readFile >> student.ID >> student.GPA)
  {
    students.push_back( student );
  }
(using arrays)
1
2
3
4
5
6
7
8
  Student students[ MAX_STUDENTS ];
  int num_students = 0;

  while (readFile >> students[num_students].ID >> students[num_students].GPA)
  {
    num_students += 1;
    if (num_students == MAX_STUDENTS) break;
  }
(If you aren't allowed to use structs, use parallel arrays)
1
2
3
4
5
6
7
8
9
  int    student_IDs [ MAX_STUDENTS ];
  double student_GPAs[ MAX_STUDENTS ];
  int    num_students = 0;

  while (readFile >> student_IDs[ num_students ] >> student_GPAs[ num_students ])
  {
    num_students += 1;
    if (num_students == MAX_STUDENTS) break;
  }

And so on.

Hope this helps.
Alright that does help but can you type how to connect the structure and the missing body parts to complete the full program with all the parameters listed by the person who asked the question basically can you type out the whole program?
No. You need to know how to do this stuff. I haven't provided you with anything more than you provided me.
me and that guy have the same assignment though. If anything how do you read the files into the structure?
I've already answered that question.
Topic archived. No new replies allowed.