A few questions about my project
Hello all. I am new to C++ programming and using Microsoft Visual Studio as my IDE. My first problem is I am unable to find the "Win32" template under C++ in the Microsoft Visual Studio 2017 Community Edition that is required of me to complete this project. With that being said, I went ahead and selected a "Empty Project" template to just begin coding so I have at least something. My second question is I am having trouble running the code from the command line. I am unsure if it is a logic error somewhere or if I need to get the Win32 template in order for this particular project to work.
The project requires me to read in student ID numbers and then a series of test scores that will be averaged after all of the data has been read in. I will display my code below in order to see if I am going about this correctly and how I can get the Win32 template downloaded so I can actually run my code.
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
ifstream in ("data1.txt");
ofstream out;
const int rows = 50;
const int cols = 5;
void read_input(ifstream &in, float IDs[], float scores[][cols], int &num_students)
{
float studentID;
float grade1, grade2, grade3, grade4;
while (true) {
in >> studentID;
if (studentID < 0) {return;}
in >> grade1;
in >> grade2;
in >> grade3;
in >> grade4;
IDs[num_students] = studentID;
scores[num_students][0] = grade1;
scores[num_students][1] = grade2;
scores[num_students][2] = grade3;
scores[num_students][3] = grade4;
num_students++;
}
}
void bubble_sort(int A[rows][cols], int size, int keyindex)
{
int i, j;
int buff1, buff2, buff3;
for (i = 0; i < (size - 1); i++)
for (j = 0; j < (size - i - 1); j++)
if (A[j][keyindex] < A[j + 1][keyindex])
{
buff1 = A[j][0];
buff2 = A[j][1];
buff3 = A[j][2];
A[j][0] = A[j + 1][0];
A[j][1] = A[j + 1][1];
A[j][2] = A[j + 1][2];
A[j + 1][0] = buff1;
A[j + 1][1] = buff2;
A[j + 1][2] = buff3;
}
}
void print_studentInfo(ofstream &outfile, float IDs[], float scores [][5], int &num_students)
{
out << "Student ID Test 1 Test 2 Test 3 Test 4" << endl;
out << "---------- ------ ------ ------ ------" << endl;
for (int i = 0; i < num_students; i++)
{
outfile << setw(7) << IDs [i] << " " << setw(7) << scores[i][1] << " " << setw(7) << scores[i][2] << " " << setw(7) << scores[i][3] << " " << setw(7) << scores[i][4] << " " << setw(7) << scores[i][5] << endl;
}
out << endl << endl << endl;
}
int main()
{
float IDs[50];
float scores[rows][cols];
int num_students = 0;
read_input(in, IDs, scores, num_students);
print_studentInfo(out, IDs, scores, num_students);
//bubble_sort(student_records, num_students, 1);
return 0;
}
I am just trying to see if this code will actually read in the numbers and put them into the array and then display them.
Re: A few questions about my project
Quote:
Originally Posted by
InvolvedOak
Hello all. I am new to C++ programming and using Microsoft Visual Studio as my IDE. My first problem is I am unable to find the "Win32" template under C++ in the Microsoft Visual Studio 2017 Community Edition that is required of me to complete this project.
Maybe this discussion could help you:
https://developercommunity.visualstu...ew-projec.html
Quote:
Originally Posted by
InvolvedOak
... I went ahead and selected a "Empty Project" template to just begin coding so I have at least something. My second question is I am having trouble running the code from the command line. I am unsure if it is a logic error somewhere or if I need to get the Win32 template in order for this particular project to work.
Unfortunately your code is very hard to read/understand. It is because you didn't use the CODE tags for proper formatting.
Please, read the Announcement: Before you post.... section
Re: A few questions about my project
As Victor mentioned in post #2, please use code tags when posting code so that the code is readable. Go Advanced, select the formatted code and click '#'.
Some comments.
You are not checking that the input file has been opened properly. Also, as you are passing ifstream and ofstream as function parameters, why are you having them as global variables - usage of global variables is not considered good practice. In this case it would be better if in and out were defined in main(). You have not provided a file name for the output file - so the output goes where? You also have some issues with the print_studentInfo() function which should be obvious when you run the code and view the output.
Why is studentID a float - I would have thought type int if it is a number?
Quote:
so I can actually run my code
Quote:
My second question is I am having trouble running the code from the command line
This is a c++ console project, so you don't need anything else. Templates only provide initial code for a particular type of project. They aren't involved in the compilation/execution of the code. You should be able to just compile this code, produce an .exe file and be able to run this.
What problems are you having compiling/running the code?
Quote:
My first problem is I am unable to find the "Win32" template under C++ in the Microsoft Visual Studio 2017 Community Edition that is required of me to complete this project
For a console project, the template you want is Windows Console Application. See the link in Victor's post #2.
PS. What is showing in the Solution Explorer Window in VS2017? Under your project name, you should see a branch called Source files and if that is expanded you should see a branch of the name of the source file (which should end with .cpp). If you don't see this you need to add the name of your c++ source file to Source files so that the compiler knows what files to use for compilation.