Thread: Reading a Whole Word in C

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19

    Reading a Whole Word in C

    SOLVED!!


    Hello, I cannot seem to find a way to get my program to read a WHOLE word and then so something according to that word.

    Basically, my program needs to ask the user about 3 options, to multiply, sum, or exit.

    I thought of doing something like this:

    Code:
    scanf("%c", &option);
    switch(option)
      {
             case 'm': printf("%f * %f = %f\n", a,b,c); break;
             ... .... ...
             default : printf("Error!\n); return 0;
      }
    But now, I want it to read the whole word, not the first character, I can't find my way around this, Help!
    Last edited by Chinfrim; 10-19-2008 at 12:55 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    To read an input string of characters option needs to be an array of characters for ex.
    Code:
    char option[100];
    scanf("%s", option);
    As you need to perform arithmetic operations on your input it's better to make option a variable of type int or float.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Quote Originally Posted by itCbitC View Post
    To read an input string of characters option needs to be an array of characters for ex.
    Code:
    char option[100];
    scanf("%s", option);
    As you need to perform arithmetic operations on your input it's better to make option a variable of type int or float.


    Oh.. Dumb Me..

    Thanks, it compiles now and all, had to use some strcmp's to get it working.

    Now to define the algorithm for factorial numbers. (n!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Microsoft Word documents
    By Mavix in forum C# Programming
    Replies: 8
    Last Post: 08-04-2007, 07:39 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  4. Reading in a file word by word
    By Bumblebee11 in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 09:39 PM