Hello guys i have encountered a little issue. I am doing a program which suppose to take up to a 1000 lines of code and print them backwards.
Here is my test code.
#include <iostream>
#include <string>
usingnamespace std;
int readData(string line[]){
int numLine=0;
int i=1;
cin>>line[0];
while (i<10){
numLine++;
getline(cin,line[i]);
i++;
}
return numLine;
}
void printLine(string line[], int x){
while (x>=0)
{
cout<<"\n";
cout<<line[x];
x--;
}
cout<<"\n";
}
int main(){
int x;
string line[1000];
x=readData(line);
printLine(line,x);
return 0;
}
}
when running a program the output prints in a weird manner. Last output is for some reason starts with a new line.....here is an example its working until last lane. and than it does new line and screws up the output. Please give advice.
aaaaaa
sssssssss
dddddddddddd
fffffffffffffff
sssssssssssssssssss
asdasdas asdasdasd
asdasdasdasda sadasdasdasd
asdasd
dd
The other problem, I think, is the difference between cin>> and getline (lines 10 and 13). getline discards the \n from the buffer, while cin>> does not. If I have them all the same, it seems to work for me