how get a stream value

Hello!
I hope you can help me

I'm creating severus program in c++ (usgin dev).
I need help for 2 things:

1) i need get a stream from prompt.
EX:
Delay.exe

int main(){
int i;
cin>>i;
i*=1000;
Sleep(i);
}

i would like do, in the prompt, a thing like: Delay 5
Instated:
Delay
5


2) How can i use GETLINE (or similar) to get a file line?? i use getline(ifstrem,string).. but i would use a "char[]" not a "string"


THANKS ALL OF YOU
2) Use stream own getline function to read into c-strings:
http://en.cppreference.com/w/cpp/io/basic_istream/getline

1) I do not understand, can you clarify what do you mean?
Last edited on
1) when i'm in the prompt and i call DELAY.exe i would like write the value on his right and then press ENTER.
In this situation i must call delay, press enter and then digit the value (and re-press enter)

2) i need get a line from a file (for this i used getline(ifstream&, string&)) but i need use a CHAR[] without A string type

THANKS for the Answers
2) I already answered that question and provided a link to the stream own version which takes a c-string (or null terminated character array, as it is called properly) as parameter.

1) Use command line arguments:
http://www.cplusplus.com/articles/DEN36Up4/
http://www.learncpp.com/cpp-tutorial/713-command-line-arguments/

You will need to use atoi to convert passed string to integer: http://en.cppreference.com/w/cpp/string/byte/atoi
thanks!!!

I'll study all materials and i let you know!!

thankyou very much
Thanks you
it was really usefull!!!!

But now i have anotherone problem..... i think i'm haveing a bad manage code... but i cannote found th problem:

//estract funz //take a piece of a string between a, b
char * estraistring(const char*riga, int a, int b){
int dimout=b-a+2;
char * out=new char[dimout];

for (int i=a, j=0; i<b+1; i++, j++)
out[j]=riga[i];
out[dimout-1]='\0';
return out;
}


//MAIN
.........
char *estratto;
in.getline(riga, 200);
while (!in.eof()){
in.getline(riga, 200);
estratto=estraistring(riga, 13, 15);
while (strcmp(estratto, "-->")){
out<<riga<<endl;
delete[]estratto;
in.getline(riga, 200);
estratto=estraistring(riga, 13, 15);
}
delete[]estratto;
edittime(riga, tempo, 6);
edittime(riga, tempo, 23);
out<<riga<<endl;
}

out.close();
in.close();
}//END MAIN



1) is necessary making a "char*estratto" in order to delete the funz's product???

2) my problem is that the while never find the EOF()

If i need open a new thread, tell me.

Thanks, a lot.
1) You should store result of function somewhere to be able to delete it.
2) If your last line contains "-->" in expected place, following happens:
getline tries to extract a string from stream and either extracts no characters due to fact that stream contains no more characters. EOF is set here. original string is not changed.
estraistring is called. As riga was not changed, it operates on previous line which contains "-->" on expected place, and condition while (strcmp(estratto, "-->")){ evaluates to true, running loop once again ad infinum.

Some notes: do not loop on eof(). It is almost sure way to introduce bugs in your code. Best action is to loop on input operation.
Always check the successfulness of input operation before using it result. For example:
1
2
3
in.getline(riga, 200);
//You should check if getline was succesfull here before using riga on second line
estratto=estraistring(riga, 13, 15);
1) didn't i did it?
when i make a string and i assegn it on a pointer, deleting on pointer (delete[estratto]) the string 's space shoud be free.. or not??

2) i get it!! thanks!!

3) can you make me some examples on your suggesting?
"Some notes: do not loop on eof(). It is almost sure way to introduce bugs in your code. Best action is to loop on input operation."


4) infact i having problem about this
in.getline(riga, 200);
//You should check if getline was succesfull here before using riga on second line
estratto=estraistring(riga, 13, 15);

i don't know if is a funz(estraistring) problem that gave me a corrupt string (but i don't think) or if the operation getting faild (i didn't know it could be fail)

How can i check??? i don't know.... and WHY IT COULD NOT WORK?






Thanks for your time
didn't i did it?
Yes you did. It was an answer to question "is necessary making a "char*estratto" in order to delete the funz's product???"

Canonical way to loop on input:
1
2
3
4
5
6
7
8
9
while(in.getline(riga, 200)) {//Loop while we succesfully read lines
    estratto=estraistring(riga, 13, 15);
    if(not strcmp(estratto, "-->")) {
        edittime(riga, tempo, 6);
        edittime(riga, tempo, 23);
        out<<riga<<endl;
    }
    delete[] estratto;
}
Your code is slime version of mine, GREAT!!!

Thanksfor your help.

I found the bug:
sometime when a RIGA was short and i asked its 13%15 position (that it hasn't) the stream (dirty) read --> of a maybe previously RIGA so it entered in "editime". I put a check on strlen.

Thanks for your help.

P.S. why in.getline(char*, int); sometime doesen't read (like ur lerned me)???
What if there is nothing to read? Like you read whole file already? What you think it would do?
So i'm not getting wrong when i feel dumb.

Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Topic archived. No new replies allowed.