Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Entering a sentence into an array in C

  • 18-10-2006 7:25pm
    #1
    Closed Accounts Posts: 20


    Hey all,

    I am trying to write a bit of code to enter a string into an array, but to only stop when the user enters a new line with only a '.' on it.

    At the moment i have:

    printf("Enter a sentence:\n");
    while(sentence!='.')
    {
    scanf("%[^\n]s",&sentence);
    i++;
    }

    This just stops when you hit enter. So how do you get it to stop taking input only after the '.'

    Any help would be great, thanx


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Well, for one your inputting a character, then jumping up along the index. Then you check the new index position, which will always be blank. Increment before you scan the new character (unless of course the index is zero).


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Declare the array first perhaps?


  • Closed Accounts Posts: 20 Zedd


    That was just a segment of the code, all the variables and the array have been define before hand. I can put the lot up if it will help?

    I see what you mean about the increment but it still just stops taking the input when you hit enter. Any ideas on how to get it to allow you to hit enter if you like until enter '.'


  • Registered Users, Registered Users 2 Posts: 6,570 ✭✭✭daymobrew


    Would getch() be better than scanf because it doesn't wait for Enter to be pressed before returning. It's been so long since I used this function I can't give you sample code. Try Google's Code Search.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 93,567 Mod ✭✭✭✭Capt'n Midnight


    Zedd wrote:
    Hey all,

    I am trying to write a bit of code to enter a string into an array, but to only stop when the user enters a new line with only a '.' on it.
    OMG no buffer overflow protection :eek: :eek:

    /broken record ;)

    do you have to check the length of getch() or maybe I'm confusing it with another way that returns two bytes on special keys ?


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    UCD - 2nd year engineering. You have Heneghan, right?

    Anyway, as a hint (this is how i solved the question when i did it): Think of what the last few key presses will be when the user is trying to finish entering a sentance. Type in a sentance, and look at the last few keypresses as you "complete" your sentence.

    Once you know what those keypresses are, you just have to run a check every time a char is read in to make sure that sequence isn't at the end of your array.


Advertisement