char arrays

This is a discussion on char arrays within the C++ Programming forums, part of the General Programming Boards category; how can i get my programme to read the first character eg, if i have a programme that asks the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    char arrays

    how can i get my programme to read the first character eg, if i have a programme that asks the user to input a word, then reads the word and checks if the first chacter is a vovel

    #include <stdlib.h>
    #include <stdio.h>
    int main (void)
    {

    char a,e,i,o,u, word[50];
    char letter;
    printf("input a word: ");
    scanf("%s", &word);

    letter = word[0];
    printf("%s\n",letter);



    system ("pause");
    return 0;
    }


    can you tell me where the mistake is?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You're using the wrong format specifier to output the character. %s is for strings.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    #include <stdio.h>
    this header is for C IO routines like printf scanf

    Code:
    cin >>
    and these are C++ routines which require <iostream> header

    void main() is not standard - see FAQ

    instead of C-strings better to use std::string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Be careful when reading string with scanf; it's unsafe by default. SourceForge.net: Scanf woes - cpwiki
    And be sure to read stickies! << !! Posting Code? Read this First !! >>
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21