Connecting Tech Pros Worldwide Forums | Help | Site Map

strange problem

Pitaridis Aristotelis
Guest
 
Posts: n/a
#1: Oct 20 '06
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



mlimber
Guest
 
Posts: n/a
#2: Oct 20 '06

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

Tim Love
Guest
 
Posts: n/a
#3: Oct 20 '06

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


red floyd
Guest
 
Posts: n/a
#4: Oct 20 '06

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.
Stuart Redmann
Guest
 
Posts: n/a
#5: Oct 20 '06

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
Default User
Guest
 
Posts: n/a
#6: Oct 20 '06

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

red floyd
Guest
 
Posts: n/a
#7: Oct 20 '06

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++.
jjds101@yahoo.com
Guest
 
Posts: n/a
#8: Oct 20 '06

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}
}

Pitaridis Aristotelis
Guest
 
Posts: n/a
#9: Oct 20 '06

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
>
>

BobR
Guest
 
Posts: n/a
#10: Oct 21 '06

re: strange problem



Pitaridis Aristotelis wrote in message <1161375406.882341@athprx03>...
Quote:
>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
PLEASE, do not top-post!!!

The 'error explanations' are NOT awful! YOU must learn!!!

Get "Thinking in C++", 2nd ed. Volume 1 (&2) by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/

--
Bob R
POVrookie
--
Dev-C++ IDE: http://www.bloodshed.net/
MinGW (GNU compiler): http://www.mingw.org/
MinGWStudio http://www.parinyasoft.com/
wxWidgets URL: http://www.wxwidgets.org
V IDE & V GUI: http://www.objectcentral.com/


Closed Thread