What's wrong with this code?

Assume that "userinfo.txt" contain the following text.



abc 1234 90



The code is shown below:

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
62
63
64
65
#include<fstream>
#include<iostream>

using namespace std;
char* save_data(char*username);

int main()
{	char abc[]="abc";
	char *s;
	s=save_data(abc);
	cout<<s<<endl;
	return 0;
}

char* save_data(char*username)
{
	char name[30];
	char score[8];
	char ch = 0;

	bool check_name;
	bool end=0;
	int pt;
	int postion;

	ifstream fin("userinfo.txt");

	while(!end)
	{

		for(int i=0;(ch = fin.get()) != '\t';i++)//get the name
		{
			name[i] = ch;
			name[i+1]= '\0';
		}

		for(int i=0;(ch = fin.get()) != '\t';i++)//skip the password
		{}

		pt=fin.tellg();
	
		for(int i=0;(ch = fin.get()) != '\n';i++)//get the score
		{
			score[i] = ch;
			score[i+1]= '\0';
		}
		postion=fin.tellg();
		fin.get();
		end=fin.eof();
		if(end!=true)
		{
			fin.seekg(postion);
		}
		check_name = strcmp(name, username);//If they are identical, return true.

		if(check_name==0)
		{			
		
			return score;

		}
	}
	fin.close();
	return 0;
}
You're trying to use strcmp without including the header (cstring) and in the function save_data you're returning the address of something that is destroyed as soon as the function ends, so you're returning the address of something that no longer exists.
what should i do if i want to return the score?

I suggest you make score a std::string and return the string, rather than a pointer to a char.
isn't it like this?
std:string score1;
score1=score;
return score;
That doesn't make much sense. Why create score1 if you don't do anything with it?

1
2
3
4
5
6
std::string save_data(char*username)
{
  std::string score;
  // all your code to put something in score
  return score;
}


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
62
63
64
#include<fstream>
#include<iostream>
#include<string.h>

using namespace std;
std::string save_data(char*username);

int main()
{	char abc[]={"honamc"};
	std::string s;
	s=save_data(abc);
	cout<<s;
	return 0;
}

std::string savedata(char*username)
{
	char name[30];
	std::string score;
	char ch = 0;

	bool check_name;
	bool end=0;
	int pt;
	int postion;

	ifstream fin("userinfo.txt");

	while(!end)
	{

		for(int i=0;(ch = fin.get()) != '\t';i++)//get the name
		{
			name[i] = ch;
			name[i+1]= '\0';
		}

		for(int i=0;(ch = fin.get()) != '\t';i++)//skip the password
		{}

		pt=fin.tellg();
	
		for(int i=0;(ch = fin.get()) != '\n';i++)//get the score
		{
			score[i] = ch;
			score[i+1]= '\0';
		}
		postion=fin.tellg();
		fin.get();
		end=fin.eof();
		if(end!=true)
		{
			fin.seekg(postion);
		}
		check_name = strcmp(name, username);

		if(check_name==0)
		{			
			return score;
		}
	}
	fin.close();
	
}

isn't it like this?
still not work/.\
error at cout
Topic archived. No new replies allowed.