Inputing into a char*

Over the past few days I have been searching literally everywhere for an answer to this question: How do you output a stings contents into a char*? I am creating a username/password system were you can register yourself, or log into an account you have already registered. In the main program I have it copy the username and password onto a txt file when you register. If you log in, it searches for your username throughout the txt file and finds the next line (the password). Then it compares what you put in as your password and what your registered password is. I created this short program just testing how to find the line after a certain word. Then i realized I need to have it use a string instead of a random word in parenthesis. Here is the short program that pulls the line following what you search for. Sorry if code is messy. :(

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
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
int main () 
{
 char* search = "whatever you want it to search"; // search pattern
 int offset;
 int count; 
 string line;
 ifstream Myfile;
 Myfile.open ("example.txt");
 
 if(Myfile.is_open())
 {
  	while(getline(Myfile,line))
	{
	         count++;
	         if(search == line)
		{
		         int index = count+1;
			if(count = index)
			{
			     getline(Myfile,line);
			     cout<<"following line is .. "<<line<<endl<<endl;
			}
		}
	}
     Myfile.close();
}


The main problem is that I need to make char* search = a string so that this short program will be able to have user input from strings.

Thanks,
snydecor000
Last edited on
Yanson, I tried the string copy method and all went well until I got to the part of the program where it puts the sting into the char*. The moment you type in the username and click enter it asks to close the program due to errors. Here is the code I used:
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
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
int main () 
{
 cout << "Please enter your username.\nUsername: ";
 string usernamein;
 cin >> usernamein;
 size_t length = usernamein.copy(search,1,0); //username is only 1 word
 search[length]='\0';
 int offset;
 int count; 
 string line;
 ifstream Myfile;
 Myfile.open ("example.txt");
 
 if(Myfile.is_open())
 {
  	while(getline(Myfile,line))
	{
	         count++;
	         if(search == line)
		{
		         int index = count+1;
			if(count = index)
			{
			     getline(Myfile,line);
			     cout<<"following line is .. "<<line<<endl<<endl;
			}
		}
	}
     Myfile.close();
}

Do you have any ideas why this is happening?

Thanks,
snydecor000
Lines 12 and 13 need to be reversed. The array has to be declared before you use it.
1
2
3
4
	 const int SIZE = 100;
	 char search[SIZE];
	 size_t length = usernamein.copy(search, usernamein.length() ,0);
	 search[length] ='\0';


Line 15 needs to be initialized before you increment it int count = 0;

Line 28 if(count = index) is this supposed to be an assignment statement?
The main problem is that I need to make char* search = a string so that this short program will be able to have user input from strings.


Why don't you just use make search a string to begin with?
Maybe I'm stupid but aren't you trying to put a square peg in a round hole? Or is the string one character long? If so then just use type casting/conversion and do

char mychar;
string mystring;
mychar = mystring;

The data will be copied and the remainder (if any) will be lost, if I am not mistaken.

I didn't read your code so I apologize if this answer is useless.
Last edited on
Cire, I don't know how that would work, that is why I constructed the program the way I did. If you know how to do that, I would appreciate it if you posted a program that does what I am trying to accomplish.

Thanks,
snydecor000
Try just making search a string instead of a char*. It looks like your program would still work with the change. If there are errors afterwards, try to figure them out or post them.
Zhuge, your suggestion worked perfectly. Thanks!
Topic archived. No new replies allowed.