getline?

Okay, I have tryed to solve this problem for some hours now but can't get it correct. I still don't get the function getline to work. The last couts will appear after each other and for some reasons forget the cin getline.. I display all my code for you 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <fstream>

using namespace std;


int main(){


cout<<"Programmet kommer att starta efter 3 sekunder:"<<endl;


int i = 4;
int b = 0;

while(!kbhit() && i>b){

i--;
Sleep(1000);
cout<< i <<endl;

if(i==0){
    
cout<<" \nProgrammet har startat! "<<endl;
}
}

int val;
while(val != 1 && val !=2 && val !=3){
cout<<"\nTryck in nummer far att valja pa faljande alternativ:"<<endl;
cout<<"1. Fotboll\n2. Hastar\n3. Hockey\n"<<endl;
cin>> val;
}

if(val==1){
    cout<<"Fotboll är en sport där man är 10 man på planen och en målvakt.\n Tanken är att man skall göra mål på motsåndarlagets målvakt.\nDet laget som gör flest mål när tiden är ute vinner! "<<endl;

}


else if(val==2){

    cout<<"Horses is a sport for for girls. Not a sport that i would recomand you. "<<endl;
}

else if(val==3){

    cout<<"This is on of the sports that i really would recomand you\n The swedish team Vaxjo Lakers. "<<endl;
}
    
    
    char j;


    cout<<"\nTryck J eller N för att fortsätta eller ej."<<endl;
    cin>>j;

    if(j=='J' || j=='j' ){
        

        system("pause");


    }
    
    
        else
        return 0;

    
    

    string filnamn;
    string text;
    char lookslike;


    cout<<"\nThen let's move on! "<<endl;
    cout<<"\nEnter the file name you want to create: ";// this couts appear after ech other.
    getline(cin, filnamn);      // program forget.
    ofstream f1(filnamn);
    cout<<"Enter the text you want to include the file: "<<endl;  // this cout apear direcly after the cout above..
    getline(cin, text);
    f1 << text;
    f1.close();

    cout<<"You wanna see how the file you just created looks like? J/N"<<endl;
    cin>>lookslike;

    if(lookslike=='J' || lookslike=='j'){
        ifstream f2(filnamn);
        cout<<"This stance in the file you just created\n"<<endl;
        cout<<f2.rdbuf();
        f2.close();


    }

    else if(lookslike!='J' || lookslike != 'j'){

        
        return 0;

    }




}  
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <fstream>

using namespace std;


int main(){


cout<<"Programmet kommer att starta efter 3 sekunder:"<<endl;


int i = 4;
int b = 0;

while(!kbhit() && i>b){

i--;
Sleep(1000);
cout<< i <<endl;

if(i==0){
    
cout<<" \nProgrammet har startat! "<<endl;
}
}

int val;
while(val != 1 && val !=2 && val !=3){
cout<<"\nTryck in nummer far att valja pa faljande alternativ:"<<endl;
cout<<"1. Fotboll\n2. Hastar\n3. Hockey\n"<<endl;
cin>> val;
}

if(val==1){
    cout<<"Fotboll är en sport där man är 10 man på planen och en målvakt.\n Tanken är att man skall göra mål på motsåndarlagets målvakt.\nDet laget som gör flest mål när tiden är ute vinner! "<<endl;

}


else if(val==2){

    cout<<"Horses is a sport for for girls. Not a sport that i would recomand you. "<<endl;
}

else if(val==3){

    cout<<"This is on of the sports that i really would recomand you\n The swedish team Vaxjo Lakers. "<<endl;
}
    
    
    char j;


    cout<<"\nTryck J eller N för att fortsätta eller ej."<<endl; //// here the user types 'j' then Enter. This means in the cin stream you have 'j' and then '\n'
    cin>>j; //// this means 'j' is removed from the cin stream BUT '\n' is not

    if(j=='J' || j=='j' ){
        

        system("pause");


    }
    
    
        else
        return 0;

    
    

    string filnamn;
    string text;
    char lookslike;


    cout<<"\nThen let's move on! "<<endl;
    cout<<"\nEnter the file name you want to create: ";// this couts appear after ech other.
    getline(cin, filnamn); //// add this line. The '\n' will be removed and cin stream will be empty
    getline(cin, filnamn);      //// since cin stream is empty this cin will wait for user input
    ofstream f1(filnamn);
    cout<<"Enter the text you want to include the file: "<<endl;  // this cout apear direcly after the cout above..
    getline(cin, text);
    f1 << text;
    f1.close();

    cout<<"You wanna see how the file you just created looks like? J/N"<<endl;
    cin>>lookslike;

    if(lookslike=='J' || lookslike=='j'){
        ifstream f2(filnamn);
        cout<<"This stance in the file you just created\n"<<endl;
        cout<<f2.rdbuf();
        f2.close();


    }

    else if(lookslike!='J' || lookslike != 'j'){

        
        return 0;

    }




}  
Topic archived. No new replies allowed.