c++ save feature, string error?

Alright, so I just simply need help with this. I cannot get "mystring" to equal "correct". I would really appreciate some help in figuring this out, I am making a text adventure game and would love some tips.

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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <iostream>
#include <string>
#include <stdio.h>

std::string a;
std::string action;
std::string correct;

int main(){
	getline(std::cin, action);
	if (action == "save"){
		goto save_code_initialize;
	}
	if (action == "load"){
		FILE * pFile;
		char mystring[8];

		pFile = fopen("save.txt", "r");
		if (pFile == NULL){
			perror("Error opening file");
		}
		else{
			if (fgets(mystring, 8, pFile) != NULL){
				puts(mystring);
				mystring == correct;
			}
		}
		goto prompt;
	}
save_code_initialize:
	FILE * pfile;
	char sentence[8];

	printf("Enter your personal save code: ");
	fgets(sentence, 8, stdin);
	pfile = fopen("save.txt", "a");
	fputs(sentence, pfile);
	fclose(pfile);

	sentence == a;

	return 0;
prompt:
	std::cout << "Enter your code: ";
	getline(std::cin, action);
	if (action == correct){
		std::cout << "Code accepted\n";
	}
	else if (action != correct){
		std::cout << "Invalid code\n";
		return 0;
	}
}
correct is an empty string. Also note that == is used for comparison, and = is used for assignment.
How do I get correct to contain mystring? And If I don't use == , my compiler tells me that the expression must be a modifiable lvalue.
Yes, because you've declared sentence and mystring to be char arrays, so you can't just assign like that from a std::string to a char array. Why are you mixing char arrays and std::strings in the same program?
Last edited on
I'd guesss that for line 29 you mean correct = mystring;
Topic archived. No new replies allowed.