New to C please help nice and easy!!

H

hoggmeister

Hi,

Im new to C coming from a java background. I having difficulty
adjusting to C and was hoping someone could help me with a little
simple code to get started. I would like a little program that outputs
on to the console a message like "please enter some text" then when the
user enters text it gets stored in a char array or whatever is best. I
then want to check that the array is no longer than 25 chars ( i dont
know if malloc is required or not ) and if it is less than 25 it gets
stored in virtual memory if not then it loops and asks for input again.
Just a nice easy one so i can put in debugger to understand how the
code works. Thanks for looking.
 
T

Thomas Matthews

Hi,

Im new to C coming from a java background. I having difficulty
adjusting to C and was hoping someone could help me with a little
simple code to get started. I would like a little program that outputs
on to the console a message like "please enter some text" then when the
user enters text it gets stored in a char array or whatever is best. I
then want to check that the array is no longer than 25 chars ( i dont
know if malloc is required or not ) and if it is less than 25 it gets
stored in virtual memory if not then it loops and asks for input again.
Just a nice easy one so i can put in debugger to understand how the
code works. Thanks for looking.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void)
{
string data;
static vector<string> virtual_memory;
while (true)
{
cout << "please enter some text";
getline(cin, data);
if (data.length() >= 25)
{
virtual_memory.push_back(data);
}
} // End: while true
return 0;
}



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
V

vijay

# include<stdio.h>

int main()
{
char str[100];
do{
printf("Enter text: \n");
fgets(str,sizeof(str),stdin);

if(strlen(str)>=25)
continue;
else{
printf("\nThe text is: %s",str);
break;
}
}
while(1);

return 0;
}
 
E

Eric Sosman

Thomas said:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void)
{
string data;
static vector<string> virtual_memory;
while (true)
{
cout << "please enter some text";
getline(cin, data);
if (data.length() >= 25)
{
virtual_memory.push_back(data);
}
} // End: while true
return 0;
}

I'm having trouble getting this code to work; can
you tell me what's wrong? Here's what the compiler says:

bogus.c:1:20: iostream: No such file or directory (ENOENT)
bogus.c:2:18: string: No such file or directory (ENOENT)
bogus.c:3:18: vector: No such file or directory (ENOENT)
bogus.c:4: parse error before "namespace"
bogus.c:4: warning: type defaults to `int' in declaration of `std'
bogus.c:4: ISO C forbids data definition with no type or storage class
bogus.c: In function `main':
bogus.c:8: `string' undeclared (first use in this function)
bogus.c:8: (Each undeclared identifier is reported only once
bogus.c:8: for each function it appears in.)
bogus.c:8: parse error before "data"
bogus.c:9: warning: ISO C forbids nested functions
bogus.c:9: syntax error before '<' token
bogus.c:10: `true' undeclared (first use in this function)
bogus.c:12: `cout' undeclared (first use in this function)
bogus.c:13: warning: implicit declaration of function `getline'
bogus.c:13: `cin' undeclared (first use in this function)
bogus.c:13: `data' undeclared (first use in this function)
bogus.c:16: `virtual_memory' undeclared (first use in this function)
bogus.c:18: parse error before '/' token
bogus.c:12: warning: statement with no effect
bogus.c:20: warning: control reaches end of non-void function

(An oddity: The lines of diagnostic messages outnumber
the lines of source code.)
 
E

Eric Sosman

vijay said:
# include<stdio.h>

int main()
{
char str[100];
do{
printf("Enter text: \n");
fgets(str,sizeof(str),stdin);

if(strlen(str)>=25)
continue;
else{
printf("\nThe text is: %s",str);
break;
}
}
while(1);

return 0;
}

This works (or will, after #include <string.h> is
added), but why make the control flow so baroque?

do {
printf (...);
fgets (...);
} while (strlen(str) >= 25);
printf ("The text is: %s\n", str);

.... seems to express the intent both more clearly and
more succinctly.

Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.
 
K

Keith Thompson

Eric Sosman said:
Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.

Conjecture: You meant that "almost" can be deleted without falsifying
the observation (unless you meant "almost a mistake").
 
T

Thomas Matthews

Thomas said:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void)
{
string data;
static vector<string> virtual_memory;
while (true)
{
cout << "please enter some text";
getline(cin, data);
if (data.length() >= 25)
{
virtual_memory.push_back(data);
}
} // End: while true
return 0;
}

Sorry, brain in wrong gear.
{Having to switch between C, C++, Visual Basic, and
VBScript today. Ouch.}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

Eric Sosman

Keith said:
Conjecture: You meant that "almost" can be deleted without falsifying
the observation (unless you meant "almost a mistake").

In my own high-falutin' syntax I seem to myself
have uptripped. That'll larn me -- 'cept it probably
won't, alas ...
 
C

CBFalconer

Thomas said:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

Please don't post C++ code in c.l.c. That is a different language.
 
R

Richard Bos

Eric Sosman said:
Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.

If s/"always"/"almost"/, then yes, it can always be replaced by while
(1) { ... }, since the only difference between those two is in when the
condition is evaluated; and in these cases, the condition has no side
effects and is always true, so it doesn't matter when it's evaluated.
Since while is clearer than do...while, it is to be preferred.

Observation: this is only true for normal code. For intentionally
unclear code, such as found in the IOCCC, for example, other rules
apply.

Richard
 
T

Tor Rustad

Hi,

Im new to C coming from a java background. I having difficulty
adjusting to C and was hoping someone could help me with a little
simple code to get started. I would like a little program that outputs
on to the console a message like "please enter some text" then when the
user enters text it gets stored in a char array or whatever is best. I
then want to check that the array is no longer than 25 chars ( i dont
know if malloc is required or not ) and if it is less than 25 it gets
stored in virtual memory if not then it loops and asks for input again.
Just a nice easy one so i can put in debugger to understand how the
code works. Thanks for looking.

Get a copy of K&R2, there you will find a lot of simple and useful
examples.
 
A

AlexB

Hi,

Im new to C coming from a java background.

One of the best things you can do is pick up a
"Teach yourself C in 21 days" book.. It will give you
a good crash course in C. Then pick up a good reference
manual.

-Alex
 
B

Ben Pfaff

AlexB said:
One of the best things you can do is pick up a
"Teach yourself C in 21 days" book.. It will give you
a good crash course in C.

I haven't heard anyone recommend any such book in clc before.
 
C

CBFalconer

AlexB said:
One of the best things you can do is pick up a "Teach yourself C
in 21 days" book.. It will give you a good crash course in C.
Then pick up a good reference manual.

Correction, substitute worst for best above. Simply get the
accurate and reliable "The C Programming Language" by Kernighan and
Ritchie, and go to it. It will also function adequately as a
reference manual.
 
T

Tor Rustad

AlexB said:
One of the best things you can do is pick up a
"Teach yourself C in 21 days" book..

What a bad advice... lol.

K&R2 is one of the best programming books around
(regardless of language) -- try it yourself! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top