A C method that gets a string from the user inputManaging a user accountScalability of running commands from...
Create chunks from an array
Is it appropriate to ask a former professor to order a book for me through an inter-library loan?
Professor forcing me to attend a conference, I can't afford even with 50% funding
What is Tony Stark injecting into himself in Iron Man 3?
What is the purpose of a disclaimer like "this is not legal advice"?
Automaton recognizing ambiguously accepted words of another automaton
How do we create new idioms and use them in a novel?
Is there a way to make cleveref distinguish two environments with the same counter?
Why is there an extra space when I type "ls" on the Desktop?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Are all players supposed to be able to see each others' character sheets?
Graphic representation of a triangle using ArrayPlot
What would be the most expensive material to an intergalactic society?
Trocar background-image com delay via jQuery
School performs periodic password audits. Is my password compromised?
How to copy the rest of lines of a file to another file
What can I do if someone tampers with my SSH public key?
Why restrict private health insurance?
How should I solve this integral with changing parameters?
Why aren't there more Gauls like Obelix?
How can a demon take control of a human body during REM sleep?
Difference between `nmap local-IP-address` and `nmap localhost`
Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?
A C method that gets a string from the user input
Managing a user accountScalability of running commands from user inputC library that converts integers to string and vice-versaSumming user inputGet initials from name input by useritoa with “method of complements” for negative numbers in ANSI CCustom user input functionHandling user input - follow-upScan the user input and then work with these lettersK&R Exercise 1-22. Fold (break) lines at specified column
$begingroup$
I'm new to C (and English), so don't kill me yet...
I've made a simple function that gets a string from the user and returns a char*
array with said input. I would like to know how to better utilize the C tools to adapt this piece of code to the C standards.
char *getstr()
{
unsigned char buffer[128];
char counter = -1;
for(;;)
{
unsigned char c;
buffer[++counter] = (c = (unsigned char)getchar()) == 'n' ? '' : c;
if(buffer[counter] == (unsigned char)'' || counter == 127)
{
buffer[++counter] = (unsigned char)'';
break;
}
}
unsigned char str[counter];
return strncpy(str, buffer, counter + 1);
}
c reinventing-the-wheel
New contributor
$endgroup$
add a comment |
$begingroup$
I'm new to C (and English), so don't kill me yet...
I've made a simple function that gets a string from the user and returns a char*
array with said input. I would like to know how to better utilize the C tools to adapt this piece of code to the C standards.
char *getstr()
{
unsigned char buffer[128];
char counter = -1;
for(;;)
{
unsigned char c;
buffer[++counter] = (c = (unsigned char)getchar()) == 'n' ? '' : c;
if(buffer[counter] == (unsigned char)'' || counter == 127)
{
buffer[++counter] = (unsigned char)'';
break;
}
}
unsigned char str[counter];
return strncpy(str, buffer, counter + 1);
}
c reinventing-the-wheel
New contributor
$endgroup$
add a comment |
$begingroup$
I'm new to C (and English), so don't kill me yet...
I've made a simple function that gets a string from the user and returns a char*
array with said input. I would like to know how to better utilize the C tools to adapt this piece of code to the C standards.
char *getstr()
{
unsigned char buffer[128];
char counter = -1;
for(;;)
{
unsigned char c;
buffer[++counter] = (c = (unsigned char)getchar()) == 'n' ? '' : c;
if(buffer[counter] == (unsigned char)'' || counter == 127)
{
buffer[++counter] = (unsigned char)'';
break;
}
}
unsigned char str[counter];
return strncpy(str, buffer, counter + 1);
}
c reinventing-the-wheel
New contributor
$endgroup$
I'm new to C (and English), so don't kill me yet...
I've made a simple function that gets a string from the user and returns a char*
array with said input. I would like to know how to better utilize the C tools to adapt this piece of code to the C standards.
char *getstr()
{
unsigned char buffer[128];
char counter = -1;
for(;;)
{
unsigned char c;
buffer[++counter] = (c = (unsigned char)getchar()) == 'n' ? '' : c;
if(buffer[counter] == (unsigned char)'' || counter == 127)
{
buffer[++counter] = (unsigned char)'';
break;
}
}
unsigned char str[counter];
return strncpy(str, buffer, counter + 1);
}
c reinventing-the-wheel
c reinventing-the-wheel
New contributor
New contributor
New contributor
asked 6 mins ago
StacklysmStacklysm
11
11
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Stacklysm is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215122%2fa-c-method-that-gets-a-string-from-the-user-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Stacklysm is a new contributor. Be nice, and check out our Code of Conduct.
Stacklysm is a new contributor. Be nice, and check out our Code of Conduct.
Stacklysm is a new contributor. Be nice, and check out our Code of Conduct.
Stacklysm is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215122%2fa-c-method-that-gets-a-string-from-the-user-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown