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.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <fstream>
usingnamespace 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;
}
elseif(val==2){
cout<<"Horses is a sport for for girls. Not a sport that i would recomand you. "<<endl;
}
elseif(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");
}
elsereturn 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();
}
elseif(lookslike!='J' || lookslike != 'j'){
return 0;
}
}
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <fstream>
usingnamespace 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;
}
elseif(val==2){
cout<<"Horses is a sport for for girls. Not a sport that i would recomand you. "<<endl;
}
elseif(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");
}
elsereturn 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();
}
elseif(lookslike!='J' || lookslike != 'j'){
return 0;
}
}