strange problem 
October 20th, 2006, 12:55 PM
| | | strange problem
can someone tell me why the following program does not display the text that
have been collected?
#include <stdio.h>
int main()
{
char c, sentence[80];
int i = 0;
puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
{
sentence[i] = c;
i++;
}
sentence[i] = '\0'; /* Insert null at end of string */
puts("\nThe line entered was :");
puts(sentence);
return 0;
}
Aristotelis | 
October 20th, 2006, 01:05 PM
| | | Re: strange problem
Pitaridis Aristotelis wrote: Quote:
can someone tell me why the following program does not display the text that
have been collected?
>
#include <stdio.h>
>
int main()
{
char c, sentence[80];
int i = 0;
>
puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
| That line should not have a semicolon. Quote:
{
sentence[i] = c;
i++;
}
>
sentence[i] = '\0'; /* Insert null at end of string */
>
puts("\nThe line entered was :");
puts(sentence);
>
return 0;
}
| But there are other potential problems. Why aren't you using
std::string instead of raw arrays
( http://www.parashift.com/c++-faq-lit...tml#faq-34.1)?
Cheers! --M | 
October 20th, 2006, 01:15 PM
| | | Re: strange problem
"Pitaridis Aristotelis" <pitaridis@hotmail.comwrites: Quote:
>can someone tell me why the following program does not display the text that
>have been collected?
| Because you've an extra ';' at the end of the 'while' line.
See http://www-h.eng.cam.ac.uk/help/tpl/...+/FAQ.html#1.1 | 
October 20th, 2006, 02:55 PM
| | | Re: strange problem
mlimber wrote: Quote: |
Pitaridis Aristotelis wrote:
| Quote: Quote: |
> while ((c = getchar()) != '\n' );
| >
That line should not have a semicolon.
> Quote:
> {
> sentence[i] = c;
> i++;
> }
>>
| | Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.
for (i = 0 ; i < N; i++);
{
/* initialize some_array[i] */
}
We've all been there, done that. | 
October 20th, 2006, 05:35 PM
| | | Re: strange problem
Pitaridis Aristotelis wrote: Quote:
can someone tell me why the following program does not display the text that
have been collected?
>
#include <stdio.h>
>
int main()
{
char c, sentence[80];
int i = 0;
>
puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
{
sentence[i] = c;
i++;
}
>
sentence[i] = '\0'; /* Insert null at end of string */
>
puts("\nThe line entered was :");
puts(sentence);
>
return 0;
}
| 1. When posting to newsgroups, please choose a telling header. 'strange
problem' is pretty non-descript, don't you think?
2. Buy yourself a decent compiler. Mine will issue a warning in above cases.
3. Why don't you try to debug your code? Tracing once through it would
have solved this in no time.
Regards,
Stuart | 
October 20th, 2006, 05:45 PM
| | | Re: strange problem
red floyd wrote: Quote:
mlimber wrote: Quote: |
Pitaridis Aristotelis wrote:
| > Quote: Quote: |
while ((c = getchar()) != '\n' );
| That line should not have a semicolon.
| | Quote:
Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.
|
When I was learning C, I had a sheet of paper with the heading, "Having
Problems Dummy?", where I recorded various gotchas that bit me. Old
Turbo C wasn't as forthcoming with warnings as some of the modern
compilers, so stuff like the above or:
if (y = 1)
{
}
Or not passing pointers to scanf(), used to pass right on through.
Brian | 
October 20th, 2006, 06:05 PM
| | | Re: strange problem
Stuart Redmann wrote: Quote:
Pitaridis Aristotelis wrote: Quote:
>can someone tell me why the following program does not display the
>text that have been collected?
>>
>#include <stdio.h>
>>
>int main()
>{
> char c, sentence[80];
> int i = 0;
>>
> puts("Enter a line of text:\n");
> while ((c = getchar()) != '\n' );
> {
> sentence[i] = c;
> i++;
> }
>>
> sentence[i] = '\0'; /* Insert null at end of string */
>>
> puts("\nThe line entered was :");
> puts(sentence);
>>
> return 0;
>}
| >
1. When posting to newsgroups, please choose a telling header. 'strange
problem' is pretty non-descript, don't you think?
>
2. Buy yourself a decent compiler. Mine will issue a warning in above
cases.
>
3. Why don't you try to debug your code? Tracing once through it would
have solved this in no time.
>
| You forgot:
4. Post this to comp.lang.c, as this is pure C, not C++. | 
October 20th, 2006, 06:05 PM
| | | Re: strange problem
Default User wrote: Quote:
red floyd wrote:
> Quote:
mlimber wrote: Quote: |
Pitaridis Aristotelis wrote:
| Quote:
> while ((c = getchar()) != '\n' );
>
That line should not have a semicolon.
| | > Quote:
Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.
| >
>
When I was learning C, I had a sheet of paper with the heading, "Having
Problems Dummy?", where I recorded various gotchas that bit me. Old
Turbo C wasn't as forthcoming with warnings as some of the modern
compilers, so stuff like the above or:
>
if (y = 1)
{
}
>
>
Or not passing pointers to scanf(), used to pass right on through.
>
| When I was in college I spent countless hours trying to fix a bug that
I finally traced to something like:
if (some.condition()); {
//of course this block always executed because of that extraneous
semicolon}
} | 
October 20th, 2006, 08:05 PM
| | | Re: strange problem
You are right about the subject. I am sorry.
I am using the Dev-C++ because I heard that it is the best freeware
environement, but the error explanations are awfull. Can you suggest an
other environement for C++ developement?
Aristotelis
Ï "Tim Love" <tpl@eng.cam.ac.ukÝãñáøå óôï ìÞíõìá
news:ehaj9b$llv$1@gemini.csx.cam.ac.uk... Quote:
>
"Pitaridis Aristotelis" <pitaridis@hotmail.comwrites:
> Quote:
>>can someone tell me why the following program does not display the text
>>that
>>have been collected?
| Because you've an extra ';' at the end of the 'while' line.
>
See http://www-h.eng.cam.ac.uk/help/tpl/...+/FAQ.html#1.1
>
>
| | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|