wrong in c++ ??!

hello , I just found some thing wrong in c++ .. I want to know if there is any reason ..

when I wrote this in a gedit(text editor) alone with main .. it works fine , but when I try to copy it and paste it to same editor but with other function it does not work ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<fstream>
using namespace std;
int main(){
	ofstream file("yasser.txt");
        string t;
        getline(cin,t);
        int i=0;
        while (t[i]!='\0')
        {
        if (t[i]==' '){
        t[i]='_';
                        }
                i++;
        }
           file<<t;
}




but when i write this : ( piece of my function )


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

string text;

case 1:
         
	cout<<"  	 	(428000469)"<<endl;
	cout<<"  	(428000097)"<<endl;	
	cout<<"           (428000446)"<<endl;
	cout<<" Write one USER ID To send to  : " ;
	cin>>to;

	          if ( to == 428000469 )
		      {
			count++;
			ofstream mailsF;
			mailsF.open("Fisal.txt",ios::app);
			     if ( mailsF.fail() )
			     {
			             cout<<"ERROR"<<endl;
			             exit(7);
			     }
			        // mailsF<<count<<endl;
                                 //cout<<"SUBJECT : ";
                                 //cin>>sub;
                                
                                  //mailsF<<"FROM : "<<user<<endl;
                                  //mailsF<<"Subject: "<<sub<<endl;;
                 cout<<"TEXT:";
            getline(cin,text);
					
        int i=0;
        while (text[i]!='\0')
        {
        if (text[i]==' '){
        text[i]='_';
                        }
                i++;
}                                
mailsF<<"Text :"<<text<<endl;        
				 
		      }







First one allows me to write in file a string with spaces and convert all spaces to _ ...

i did the same thing with the second one , but the compiler just ignores getline and while ??

why it works there and it doesn't there !!! ?



or please tell me How can I write in file a sentence with spaces ? I mean the user do .. ?


thank you ..!
Last edited on
Watch your quotes. It helps to use an editor with syntax highlighting. Look at the code sections in your post. String literals are in darkish red.
is that what u mean ?

thanks for your note .
not sure what the solution is, but try and space out your text.

like cout << "Text : " << endl;

instead of cout<<"Text :"<<endl;

it looks way more organized my way :p
And also, the indentation is erratic and poor. Be consistent in the size of your tabs and indent in the right direction, every time.
Topic archived. No new replies allowed.