Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Simple imput/output program.

  • 07-10-2009 02:25PM
    #1
    Registered Users, Registered Users 2 Posts: 6,054 ✭✭✭


    #include<stdio.h>
    #include <conio.h>
    main()
    {

    char name ;
    char andy;

    printf( "please enter your name?");
    scanf( "%s", &name );
    if ( name == "andy" )
    printf("andys a cool name?") ;
    else
    printf("bet you wish your name was andy...\n");

    getch();

    }

    Error E2034 name2.c 10: Cannot convert 'char' to 'char *' in function main()

    Warning W8080 name2.c 17: 'andy' is declared but never used in function main()

    They are the errors that i get. What am i doing wrong in the code. I want to write a simple program that asks the user to imput a word, and then give a specific response based on what word they imput.

    thanks for any help:o


Comments

  • Registered Users, Registered Users 2 Posts: 610 ✭✭✭nialo


    your trying to put multiple char's into a single char.

    you need to delcare a char array to take in the input.
    char chrData;
    char strData[100];
    
    printf(" Enter Single Character  :");
    scanf("%c",&chrData);
    
    printf(" Enter Multiple Characters (max 99) :");
    scanf("%s",strData);
    
    


  • Registered Users, Registered Users 2 Posts: 2,534 ✭✭✭FruitLover


    Might be helpful to mention the language in the title in future, to save people guessing from the post...
    if ( name == "andy" )

    Another problem right here (once you've sorted out your string as pointed out above (caution: in a real application, you should make sure that no more than 99 chars get copied to the referenced memory location, rather than relying on the user not to input more than 99 chars!)). As the variable 'name' is effectively a pointer to a memory location, its value will not be equal to the string "andy". You'll need another way to compare the two strings...

    Also, your declaration of 'char andy' is superfluous.


Advertisement