A few questions about my project
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: A few questions about my project

  1. #1
    Join Date
    Apr 2017
    Posts
    2

    Lightbulb 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.
    Last edited by 2kaud; September 24th, 2017 at 05:59 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,008

    Re: A few questions about my project

    Quote Originally Posted by InvolvedOak View Post
    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 View Post
    ... 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
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    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?

    so I can actually run my code
    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?

    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.
    Last edited by 2kaud; September 24th, 2017 at 06:44 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)