How do I read a “string” as multiple chars if I don't know the length of the char sequence?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Sorry if the title is a bit confusing, I'll try to explain better here.
So basically I'm trying to write a program where the user inputs a phone number in an alphabetic form and it then gets translated in a numeric form. Like this:



Enter phone number: CALLATT
22555288


Where 2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=WXY



Also if the phone number input contains chars that are not alphabetical they should be left as they are. Which means:



Enter phone number: 1-800-COL-LECT
1-800-265-5328


Now, I know that this could be solved very easily using strings and arrays but I'm not supposed to use them for this exercise. I wrote this code:



#include <stdio.h>

int main()
char two = '2', three = '3', four = '4', five = '5', six = '6',
seven = '7', eight = '8', nine = '9';
char num;

printf("Enter a phone number: ");

do while(getchar() != 'n');

return 0;



Which doesn't obviously work. I mean, it works just fine if the user inputs a single char but not if the input is composed by more than one char. I can't really get my head around this... I know why and where my code is wrong but I can't really find an effective solution to solve the problem without using arrays.



Any help? Thanks a lot :)










share|improve this question



















  • 1





    Just a FYI: char num; ==> int num;

    – pmg
    Nov 15 '18 at 11:21

















0















Sorry if the title is a bit confusing, I'll try to explain better here.
So basically I'm trying to write a program where the user inputs a phone number in an alphabetic form and it then gets translated in a numeric form. Like this:



Enter phone number: CALLATT
22555288


Where 2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=WXY



Also if the phone number input contains chars that are not alphabetical they should be left as they are. Which means:



Enter phone number: 1-800-COL-LECT
1-800-265-5328


Now, I know that this could be solved very easily using strings and arrays but I'm not supposed to use them for this exercise. I wrote this code:



#include <stdio.h>

int main()
char two = '2', three = '3', four = '4', five = '5', six = '6',
seven = '7', eight = '8', nine = '9';
char num;

printf("Enter a phone number: ");

do while(getchar() != 'n');

return 0;



Which doesn't obviously work. I mean, it works just fine if the user inputs a single char but not if the input is composed by more than one char. I can't really get my head around this... I know why and where my code is wrong but I can't really find an effective solution to solve the problem without using arrays.



Any help? Thanks a lot :)










share|improve this question



















  • 1





    Just a FYI: char num; ==> int num;

    – pmg
    Nov 15 '18 at 11:21













0












0








0








Sorry if the title is a bit confusing, I'll try to explain better here.
So basically I'm trying to write a program where the user inputs a phone number in an alphabetic form and it then gets translated in a numeric form. Like this:



Enter phone number: CALLATT
22555288


Where 2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=WXY



Also if the phone number input contains chars that are not alphabetical they should be left as they are. Which means:



Enter phone number: 1-800-COL-LECT
1-800-265-5328


Now, I know that this could be solved very easily using strings and arrays but I'm not supposed to use them for this exercise. I wrote this code:



#include <stdio.h>

int main()
char two = '2', three = '3', four = '4', five = '5', six = '6',
seven = '7', eight = '8', nine = '9';
char num;

printf("Enter a phone number: ");

do while(getchar() != 'n');

return 0;



Which doesn't obviously work. I mean, it works just fine if the user inputs a single char but not if the input is composed by more than one char. I can't really get my head around this... I know why and where my code is wrong but I can't really find an effective solution to solve the problem without using arrays.



Any help? Thanks a lot :)










share|improve this question
















Sorry if the title is a bit confusing, I'll try to explain better here.
So basically I'm trying to write a program where the user inputs a phone number in an alphabetic form and it then gets translated in a numeric form. Like this:



Enter phone number: CALLATT
22555288


Where 2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=WXY



Also if the phone number input contains chars that are not alphabetical they should be left as they are. Which means:



Enter phone number: 1-800-COL-LECT
1-800-265-5328


Now, I know that this could be solved very easily using strings and arrays but I'm not supposed to use them for this exercise. I wrote this code:



#include <stdio.h>

int main()
char two = '2', three = '3', four = '4', five = '5', six = '6',
seven = '7', eight = '8', nine = '9';
char num;

printf("Enter a phone number: ");

do while(getchar() != 'n');

return 0;



Which doesn't obviously work. I mean, it works just fine if the user inputs a single char but not if the input is composed by more than one char. I can't really get my head around this... I know why and where my code is wrong but I can't really find an effective solution to solve the problem without using arrays.



Any help? Thanks a lot :)







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:59









Rai

1,1424823




1,1424823










asked Nov 15 '18 at 11:15









FoxyITFoxyIT

947




947







  • 1





    Just a FYI: char num; ==> int num;

    – pmg
    Nov 15 '18 at 11:21












  • 1





    Just a FYI: char num; ==> int num;

    – pmg
    Nov 15 '18 at 11:21







1




1





Just a FYI: char num; ==> int num;

– pmg
Nov 15 '18 at 11:21





Just a FYI: char num; ==> int num;

– pmg
Nov 15 '18 at 11:21












1 Answer
1






active

oldest

votes


















1














Try



 int num = getchar();
do

/* your if / else / if train */
printf("%c", num);

while ((num = getchar()) != 'n'); // assign and compare





share|improve this answer























  • Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

    – FoxyIT
    Nov 15 '18 at 11:48






  • 2





    Basically you were wasting 1 char for every two chars typed.

    – pmg
    Nov 15 '18 at 11:50











Your Answer






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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318246%2fhow-do-i-read-a-string-as-multiple-chars-if-i-dont-know-the-length-of-the-cha%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try



 int num = getchar();
do

/* your if / else / if train */
printf("%c", num);

while ((num = getchar()) != 'n'); // assign and compare





share|improve this answer























  • Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

    – FoxyIT
    Nov 15 '18 at 11:48






  • 2





    Basically you were wasting 1 char for every two chars typed.

    – pmg
    Nov 15 '18 at 11:50















1














Try



 int num = getchar();
do

/* your if / else / if train */
printf("%c", num);

while ((num = getchar()) != 'n'); // assign and compare





share|improve this answer























  • Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

    – FoxyIT
    Nov 15 '18 at 11:48






  • 2





    Basically you were wasting 1 char for every two chars typed.

    – pmg
    Nov 15 '18 at 11:50













1












1








1







Try



 int num = getchar();
do

/* your if / else / if train */
printf("%c", num);

while ((num = getchar()) != 'n'); // assign and compare





share|improve this answer













Try



 int num = getchar();
do

/* your if / else / if train */
printf("%c", num);

while ((num = getchar()) != 'n'); // assign and compare






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:24









pmgpmg

84.9k9100172




84.9k9100172












  • Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

    – FoxyIT
    Nov 15 '18 at 11:48






  • 2





    Basically you were wasting 1 char for every two chars typed.

    – pmg
    Nov 15 '18 at 11:50

















  • Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

    – FoxyIT
    Nov 15 '18 at 11:48






  • 2





    Basically you were wasting 1 char for every two chars typed.

    – pmg
    Nov 15 '18 at 11:50
















Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

– FoxyIT
Nov 15 '18 at 11:48





Thanks that helped. So basically as I wrote it it was listening for 2 chars at once instead of 1, right?

– FoxyIT
Nov 15 '18 at 11:48




2




2





Basically you were wasting 1 char for every two chars typed.

– pmg
Nov 15 '18 at 11:50





Basically you were wasting 1 char for every two chars typed.

– pmg
Nov 15 '18 at 11:50



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


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

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318246%2fhow-do-i-read-a-string-as-multiple-chars-if-i-dont-know-the-length-of-the-cha%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus