C新手请帮助您轻松愉快! [英] New to C please help nice and easy!!
问题描述
来自Java背景的C新手。我有困难
调整为C并希望有人可以帮我一点点简单的代码来开始。我想要一个小程序输出
给控制台一条消息,比如请输入一些文字。然后当
用户输入文本时,它会存储在char数组或其他最好的数组中。我想要检查数组不超过25个字符(我不知道
知道是否需要malloc),如果它小于25则得到
存储在虚拟内存中,如果没有那么它就会循环并再次请求输入。
只是一个很好的简单,所以我可以放入调试器来了解如何
代码有效。谢谢你的期待。
ho ********* @ gmail.com 写道:
我是C新来的一个java背景。我有困难
调整到C,并希望有人可以帮我一点点简单的代码来开始。我想要一个小程序,在控制台上输出一条消息,如请输入一些文字。然后,当
用户输入文本时,它会存储在char数组中,或者是最好的。然后我想检查一下这个数组是不是超过25个字符(我不知道是否需要malloc),如果它小于25则它会被存储在虚拟内存中如果没有那么它循环并再次请求输入。
只是一个很好的简单,所以我可以放入调试器来了解
代码的工作原理。谢谢你的期待。
#include< iostream>
#include< string>
#include < vector>
使用命名空间std;
int main(无效)
{
字符串数据;
静态向量<字符串> virtual_memory;
while(true)
{
cout<< 请输入一些文字;
getline(cin,data);
if(data.length()> = 25)
{
virtual_memory.push_back(data);
}
} //结束:while true
返回0;
}
-
托马斯马修斯
C ++新闻组欢迎消息:
http://www.slack.net/~ shiva / welcome.txt
C ++常见问题: http://www.parashift.com/c++-faq-lite
C常见问题: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c ++ faq:
http ://www.comeaucomputing.com/learn/faq/
其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库
< blockquote>Thomas Matthews <钍********** @ sbcglobal.net>写在
消息新闻:HQ ******************* @ newssvr33.news.prodigy。 com ...
ho ********* @ gmail.com 写道:
我是来自Java背景的C新手。我有困难
调整到C,并希望有人可以帮我一点点简单的代码来开始。我想要一个小程序,在控制台上输出一条消息,如请输入一些文字。然后,当
用户输入文本时,它会存储在char数组中,或者是最好的。然后我想检查一下这个数组是不是超过25个字符(我不知道是否需要malloc),如果它小于25则它会被存储在虚拟内存中如果没有那么它循环并再次请求输入。
只是一个很好的简单,所以我可以放入调试器来了解
代码的工作原理。谢谢你的期待。
#include< iostream>
那不是C.
< snip>
#include< stdio.h>
int main()< br $>
{
char str [100];
do {
printf("输入文字:\ n) ;);
fgets(str,sizeof(str),stdin);
if(strlen(str)> = 25)
继续;
else {
printf(" \\\
文字是:%s",str);
break ;
}
}
而(1);
返回0;
}
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.
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
"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>
# 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;
}
这篇关于C新手请帮助您轻松愉快!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!