473,508 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to C please help nice and easy!!

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.

Nov 14 '05 #1
16 1562
ho*********@gmail.com wrote:
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
Nov 14 '05 #2
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:HQ*******************@newssvr33.news.prodigy. com...
ho*********@gmail.com wrote:
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>


That's not C.

<snip>
Nov 14 '05 #3
# 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;
}

Nov 14 '05 #4


Thomas Matthews wrote:
ho*********@gmail.com wrote:

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


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

--
Er*********@sun.com

Nov 14 '05 #5


vijay wrote:
# 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.

--
Er*********@sun.com

Nov 14 '05 #6
Eric Sosman <er*********@sun.com> writes:
[...]
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").

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Thomas Matthews wrote:
ho*********@gmail.com wrote:
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;
}


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
Nov 14 '05 #8


Keith Thompson wrote:
Eric Sosman <er*********@sun.com> writes:
[...]
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").


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

--
Er*********@sun.com

Nov 14 '05 #9
wow i posted this not so long ago thanks for the response guys

Nov 14 '05 #10
Thomas Matthews wrote:
ho*********@gmail.com wrote:
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;


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

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #11
Eric Sosman <er*********@sun.com> wrote:
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
Nov 14 '05 #12
<ho*********@gmail.com> wrote in message
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.

--
Tor <torust AT online DOT no>
"C is not a big language, and it is not well served in a big
book". --K&R2

Nov 14 '05 #13
On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
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
Nov 14 '05 #14
AlexB <Ba****************@yahoo.com> writes:
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.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #15
AlexB wrote:
On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
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.


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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #16
"AlexB" <Ba****************@yahoo.com> wrote in message
On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
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..


What a bad advice... lol.

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

--
Tor <torust AT online DOT no>
"All novices have trouble with this aspect of linking until the concept
is explained. Then they just have trouble with the concept itself." PvdL

Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1695
by: mark | r | last post by:
ive got down adding editing and deleting items form a database and searching based on criteria... ...how do i set up parent and sub categories (say for a contacts database) i.e. fred smith is an...
5
1228
by: r2_r2 | last post by:
I am very much a beginner to python. I have been working on writing a very simple program and cannot get it and was hoping someone could help me out. Basically i need to write a code to print a...
9
1218
by: Sam Martin | last post by:
naming conventions for C# constants? we're having a mild dispute as to what is better and what is more commonly used? (better = easy to see as constant, nice to look at in code and most...
3
1056
by: msnnews.msn.com | last post by:
hi there, i have the following code on my ASPX page..: <LINK id="myCSS" runat="server" href="../print.css" type="text/css" rel="stylesheet" /> how do i reference this in the behind VB code...
6
1758
by: sexygal | last post by:
I need a program to do the following: 1. Access a file where I would have a list of people each with a different number assigned to them. 2. Ask the user to input one of the numbers 3....
10
1133
by: =?Utf-8?B?Q2hpV2hpdGVTb3g=?= | last post by:
i just finished creating an asp.net application named teddybears. during the testing phase uploaded live ( ex: www.teddy.com) , i tried to purchase something and add it to my shopping cart. ...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.