Someone who can correct my code, please

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
using namespace std;
struct Student{

char surname[15];

};

void seeStudent(Student pst);
bool getStudent();
bool addStudent();
bool buyStudent();
int main() {

char ch;

void seeStudent (Student pst)
{
cout<<"Student surname: "<<pst.Cstudent<<endl;
}
bool getStudent() {

cout<<"List of student"<<endl;
bool bFlag = false;
long i,j,N;
long fSize = -1;
char ch;
product * list = NULL;
product pst = {0};
ifstream ifs
(
"d.txt",
ios::binary
);

}
if(!ifs)

else
{
ifs.seekg(0,ios::end);
fSize = ifs.tellg();
ifs.seekg(0,ios::beg);

list = new product[(N = fSize/sizeof(product))];
ifs.read((char *)list,fSize)

}
return 0;
}
You totally messed it up :D

1) use indent
2) the funktion seeStudent and getStudent is inside the function main. Put it outside.
3) you are on the right way
Hello MazeStuck,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



As Thomas Huxhorn said:
You totally messed it up :D

First off you need to give an idea or instructions of what the program should do. If this is for a class than post the instructions given to you. They are very helpful.

From your code it is not easy to figure out what you are trying to do.

To start with "conio.h".
salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.


What you may have in your "conio.h" header is likely to be quite different than the "conio.h" header file I have for my compiler. Newer versions have left out some functions.

Then I have the question is this a C or C++ program? Mixing of the code makes it hard to tell and should not be done in the first place.

There are some variables that you are using, but have not defined. Along with some syntax errors. I did notice you did define the variables, but it was in a function and not "main". It does not work that way because of scope. You will define the variables in "main" and pass them to functions that need them.

You do not define functions inside "main". You call functions from "main".

Based on what you got wrong, and this is in no particular order, have a look at these links:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/namespaces/
http://www.cplusplus.com/doc/tutorial/dynamic/
http://www.cplusplus.com/doc/tutorial/basic_io/

And this: https://www.learncpp.com/

All useful links. And there may be one or two I missed.

Hope that helps,

Andy
Hello MazeStuck,

Your code alludes to an input file. Please post the input file or at least a fair sample if it is large, so everyone will be using the same information.

Andy

Edit: elude. Right idea wrong spelling.
Last edited on
Thank`s for everyone who answer and help me, but i have error here:
void seeStudent (Student pst)
{ ////error
cout<<"cognome dello studente: "<<pst.Cstudent<<endl;
} ////error

and the last scope before "return 0" show error too.
If someone know where is problem, i ask you to explain me please what i do wrong.
Thank you in advance.
Hello MazeStuck,

Exactly where , what line of code, defines "Cstudent"? I see "surname" defined in the struct, but nothing else.

In the else statement you have list = new product[(N = fSize/sizeof(product))];. I see two problems whit this. "N" is defined in the function "getStudent", which should not be in "main" and "main" has no access to this variable. Also I am not sure if this [(N = fSize/sizeof(product))] will even work. I will have to check on that.

Something like:
1
2
N = fSize / sizeof(product)
list = new product[N];
Should work.

I do not see any input file or any attempt to correct your code. Without fixing the entire structure of your code it is a bit pointless to try and fix your errors if the program does not work.

You first need to help your-self by giving instructions or directions of what the program should do. Without some direction it is mostly a guessing game and a waste of everyone's time.

I would like to be of more help, but for now I have no idea what you are trying to do,

Hope that helps,

Andy
Thank you for answer.
The task is following:
1)Given a non-binary file save a list of students.
2)Realize function for insertion.
3)Realize search function by surname.
If you can help me wit this (I'm new to programming) , I'll be grateful for your help.
Hello MazeStuck,

This should help to get you started:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
1)Given a non - binary file save a list of students.
2)Realize function for insertion.
3)Realize search function by surname.
*/

#include <iostream>
#include <iomanip>
#include <string>

#include <fstream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

struct Student
{
	std::string s_firstName;
	std::string s_lastName;
};

// Function prototypes


int main()
{
	// 0. Define variables. Set up for opening output file.
	//    Possible variables.
	// std::string firsdtName;
	// std::string lastName;
	std::string  outFileName{ "" }; // <--- Put file name here.


	std::ofstream outFile(outFileName);

	if (!outFile)
	{
		std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
		//std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Creates a pause and is optional.
		return 1;  //exit(1);  // If not in "main".
	}


	// 1. Get input from user.
	//   First name and last name (surname) as two separate inputs. using "std::getline" would be best.
	//   Call function to get names and write to file.


	// 2. Store names in file.
	//    Store as lastName,firstName.


	// 3. Prompt for search function.


	// 4. Call search function.

	return 0;
}  // End "main"

// Search for name function. Try not to use "search". Maybe "searchNames". 


I called the "define variables" section (0) zero because you may not always know what you will be using and you will be adding as you progress.

When writing a program it is best to work in small steps or parts and test as you go, (compile and run). The numbers 1 - 4 are to give you an idea of what to do first and next.

Your instructions do have room for interpretation and some guessing. What I get form this is that you need to get names from the user and then store them in a file.

It is possible that you will not need the struct, but could just two variables defined in "main".

Should you decide to create an array or vector instead of reading the file every time you need something then the struct would come in handy.

The bit of code to open the output file is a suggestion. I use it often when working with files. "main" may not be the best place to open and use the file. Sometimes it is better to open files in the functions that need them.

Give it some work and lets see what you come up with. Post your code and we can go from there.

Hope that helps,

Andy
Topic archived. No new replies allowed.