editing user input

Hi all,

I am new to c++ and need some help to edit the user input. For example, I ask the user to enter the module code and afterward i need to take in the user input and change it to this format < "(user entered module code).dat" >


I have the following codes

//
char module [20];
char frontOfModule [1];
char formatedModule [100];
frontOfModule[0] = '"';

cout << "Enter the module code: ";
cin >> module;

strcat (module,".dat\"");
strcpy (formatedModule, frontOfModule);
strcat (formatedModule, module);

cout << formatedModule;

//

the problem is let say i enter "math1" for the input, the output of "formatedModule" is "math1.dat"math1.dat"

Can anyone please tell me what is wrong with my code? Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.
.
char module [20];
char frontOfModule []= "\"";        //initilize like this
char formatedModule [100];
//frontOfModule[0]='"';               this was causing the error   
                                    //when character array is used as string programmer must remember to  
                                    //add a null charecter [\0] at the end of the string
                                    //"  frontOfModule[0]='"';  " - doesnt add the null character to the end

cout << "Enter the module code: ";
cin >> module;

strcat (module,".dat\"");
strcpy (formatedModule, frontOfModule);
strcat (formatedModule, module);

cout << formatedModule;
.
.



but still task can be achived using less memory and time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
.
 char module [20];
 char formatedModule []= "\"";

 cout << "Enter the module code: ";
 cin >> module;

 strcat (module,".dat\"");
 strcat (formatedModule,module);


 cout << formatedModule;
.
.
ahh thank you very much! it solved my problem!
Topic archived. No new replies allowed.