Thread: Can someone help me? Probably noob question

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    Angry Can someone help me? Probably noob question

    Hi guys, i started programming recently and i have made a program recently which is pretty simple:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    char nome1[100];
    char decision[100];
    int age1;
    
    
    printf("Welcome to the Hex Project\n\a");
    printf("What is your name?\n");
    
    scanf(" %s", &nome1);
    printf("Hi %s. This is my first project i hope you like it!\n", nome1);
    
    printf("How old are you?\n");
    scanf(" %d", &age1);
    
    printf("So, are you willing to check my program with a lot of bugs? (yes/no)?\n");
    scanf(" %c", &decision);
    
    if(age1 >= 18){
    
    if(decision == 'yes'){
    printf("Thank you for your time and welcome!");
    
    }
    
    }else{
    printf("There is nothing to see here!\n\a");
    
    }
    
    
    return 0;
    
    
    Can someone tell me why when i run the program and i awnser yes to the question "So, are you willing to check my program with a lot of bugs? (yes/no)" why it does not appear "Thank you for your time and welcome" . Thank you for your time ^^

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "yes" is a string literal, 'yes' is a character constant... Note the use of double quotation marks instead of single. Also, you can't compare strings with "==", you need "strcmp()".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Some additional comments:

    Code:
    scanf(" %s", &nome1);
    You don't need the '&' here. Since you're passing an array, the array name by itself will act as a pointer to its first element.

    Also note that you're not limiting the input, so this line of code is susceptible to a buffer overflow. One way to limit the number of characters "scanf()" will read would be to hard-code the limit:

    Code:
    scanf("%99s", ...);
    I would suggest, however, you use a function more appropriate for reading a string, such as "fgets()": FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Code:
    char decision[100];
    
    // ...
    
    scanf(" %c", &decision);
    "%c" is not what you use when you want to read a string, as you have already shown you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Question
    By DanforPresidant in forum C Programming
    Replies: 3
    Last Post: 08-21-2012, 03:19 PM
  2. noob question about RAM
    By crvenkapa in forum Tech Board
    Replies: 2
    Last Post: 03-26-2010, 03:41 PM
  3. Another Noob Question
    By Nightwarrior in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2007, 01:03 PM
  4. Noob question?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-21-2006, 09:41 PM
  5. Noob Question
    By ytaipsw in forum C Programming
    Replies: 27
    Last Post: 04-27-2006, 07:49 PM

Tags for this Thread