Jump to content

Quick (and probably easy) question regarding c++

- - - - -

  • Please log in to reply
10 replies to this topic

#1
mainevent403

mainevent403

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

I am new to these forums and new to c++ but not new to programming; however, I have run into an issue with an assignment from one of my classes.

I have a c++ project (made in visual studio 2010) with 3 sources files that I'm using:
// grade.h

// version 1

const int maxsize=20;

struct Student

{

	char stuID[5];

	int midterm;

	int final;

	int quiz1;

	int quiz2;

	double average;

	char grade;

};

class Stuclass

{

	public:

		//constructor

		Stuclass();

		// read data from a file

		void input();

		// output data to a file

		void output();

	private:

		Student myclass[maxsize];

		int numberofstudents;

		// calculate the average grade and final

		// letter grade for each student

		void calculation();

};



// grade.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"

#include <iostream>

#include <fstream>

using namespace std;


void Stuclass::input()

{

	ifstream in_stream;

	in_stream.open("input.dat", ios::in);

	if(in_stream.fail())

	{

		cerr<<"Intput file opening failed.\n";

		cin.get();

		exit(1);

	}

	int x;

	// read number of students

	in_stream >> x;

	numberofstudents=x;

	for (int i=0; i<numberofstudents; i++)

	{

		// read Student ID (4-digit)

		in_stream >>myclass[i].stuID;

		// read Quiz1

		in_stream >>myclass[i].quiz1;

		// read Quiz2

		in_stream >>myclass[i].quiz2;

		// read Midterm

		in_stream >>myclass[i].midterm;

		// read Final

		in_stream >>myclass[i].final;


		// calculate the average

		myclass[i].average = (.25 * (myclass[i].quiz1 + myclass[i].quiz2)) + 

			(.25 * (myclass[i].midterm)) + 

				(.50 * (myclass[i].final));

	

		// calculate the letter grade

		if(myclass[i].average >= 90)

		{

			myclass[i].grade = 'A';

		}

		if(myclass[i].average >= 80 && myclass[i].average < 90)

		{

			myclass[i].grade = 'B';

		}

		if(myclass[i].average >= 70 && myclass[i].average < 80)

		{

			myclass[i].grade = 'C';

		}

		if(myclass[i].average >= 60 && myclass[i].average < 70)

		{

			myclass[i].grade = 'D';

		}

		if(myclass[i].average < 60)

		{

			myclass[i].grade = 'F';

		}

		

	}

	in_stream.close();

}


void Stuclass::output()

{

	ofstream out_stream("output.dat");

	if(out_stream.fail())

	{

		cout<<"Output file opening failed.\n";

		exit(1);

	}

	out_stream << "StuID\tQuiz1\tQuiz2\tMidterm\tFinal\tAverage\tFinal Grade\n";

	for(int i = 0; i< numberofstudents; i++)

	{

		out_stream << myclass[i].stuID << "\t" <<

		myclass[i].quiz1 << "\t" <<

		myclass[i].quiz2 << "\t" <<

		myclass[i].midterm << "\t" <<

		myclass[i].final << "\t" <<

		myclass[i].average << "\t" <<

		myclass[i].grade << endl;

	}

	out_stream.close();

}


void Stuclass::calculation()

{

}



#include "stdafx.h"

#include <iostream>

using namespace std;


//int _tmain(int argc, _TCHAR* argv[])

int main()

{

	Stuclass classroom();

	cin.get();

	classroom.input;

	classroom.output;

	return 0;

}

When building the source, I get 2 errors in int_tmain at classroom.input; and classroom.output; The errors both say "error C2228: left of '.input' must have class/struct/union." What I don't get is this: classroom is an Instance of my Stuclass class, so why would I get an error like this? If anyone could help me, or at least give me hints to help me learn why this happens, I would be very thankful. If possibly, provide an explanation so I know what I'm doing wrong. Thank you

#2
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
I don't know if this is the issue, but you need to make sure you include parameter syntax to a function call, even if you don't pass any parameters.

i.e.
classroom.input();

//not 

classroom.input;

Although it doesn't make sense that classroom.input would return an error. Because classroom.input should return the address of the input() function.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#3
mainevent403

mainevent403

    Newbie

  • Members
  • Pip
  • 4 posts
Yeah, i had originally tried it that way but it unfortunately yielded the same errors for me when building, thanks for the quick reply though.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

classrom.input;

classrom.input();

These are not the same in C++ (I'm pretty sure they can't even exist at the same time). First line is an attribute (variable) and second is method (class function).

Comment on the code:

int x;

// read number of students

in_stream >> x;

numberofstudents = x;

Why not like this?

in_stream >> numberofstudents;


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
mainevent403

mainevent403

    Newbie

  • Members
  • Pip
  • 4 posts
I suppose it would make sense to do it that way, but it still doesn't solve the main problem I'm having where I can't compile, thanks for the optimization tip though :].

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
What error do you get?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Your problem is the compiler is parsing this:
Stuclass classroom();
to be a forward function declaration, not a class instance. Do this instead:
Stuclass classroom;
That should fix it.
Wow I changed my sig!

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

ZekeDragon said:

Your problem is the compiler is parsing this:
Stuclass classroom();
to be a forward function declaration, not a class instance. Do this instead:
Stuclass classroom;
That should fix it.
Actually, those are the same; you can create instances either way with ctor that takes no parameters.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
mainevent403

mainevent403

    Newbie

  • Members
  • Pip
  • 4 posts
That seems to be the problem, thank you very much :]

#10
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
@Flying Dutchman: actually, ZekeDragon is right. TIAS.

#include <iostream>


struct Foo {

    int x;

    

    Foo() {

        std::cout << "Foo!\n";

    }

};


int main() {

    Foo foo(); // thinks it's forward declaration, should be Foo foo;


//    foo.x = 0; // whoops:

// cpp:14:9: error: request for member 'x' in 'foo', which is of non-class type 'Foo()'

    

    return 0;

}
This code will not print "Foo!" as you might expect. ;)

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
My bad, I assumed it works because I never had problems with this. Or maybe I mixed it with something... Anyways, thanks for pointing it out, learned something new. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users