My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Thanks Tree1Thanks
  • 1 Post By skaa
Reply
 
LinkBack Thread Tools Display Modes
December 28th, 2016, 09:49 AM   #1
Member
 
Joined: Oct 2016
From: Slovenia, Europe

Posts: 46
Thanks: 5

Question Basic problem with characters in C.

I am wondering why does the program print the request for a letter twice

I give you a photo of my problem: Share.Pho.to | This photo set was shared via Share.Pho.to


...and my code, if you cannot open the picture
Code:
#include<stdio.h>
#include<string.h>
int main(void)
{     
    while(1)
        {
        printf("letter: "); char letter; scanf("%c",&letter);
        }
    
    return 0;
}
This is what I get if I run this program and type a, b, c ...
Code:
letter: a
letter: letter: b
letter: letter: c
letter: letter: d
letter: letter: e
Please help me guys!
srecko is offline  
 
December 28th, 2016, 01:58 PM   #2
Global Moderator
 
Joined: Dec 2006

Posts: 16,602
Thanks: 1200

Have you considered checking what the letter variable contains just after the line in the loop has executed twice?

Last edited by skipjack; December 28th, 2016 at 02:14 PM.
skipjack is online now  
December 28th, 2016, 01:58 PM   #3
Senior Member
 
skaa's Avatar
 
Joined: Mar 2011
From: Chicago, IL

Posts: 211
Thanks: 76

Use scanf(" %c",&letter), this format ignores the endline.
Thanks from srecko
skaa is offline  
December 29th, 2016, 03:03 AM   #4
Member
 
Joined: Oct 2016
From: Slovenia, Europe

Posts: 46
Thanks: 5

Skaa, thank you very much. That was the key to success!
This is my code, which works as Guess the word - Hangman game
Code:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
#include <time.h>
int main(void)
{
    printf("Enter the word: "); char word[30]; scanf("%s", word);

    int l= strlen(word); //dolzina besede (l)

    char hid_word[30]; //skrita beseda
    printf("Press any key to hide the word!");
    getch();
    system("cls");
    for (int i=0;i<l;i++)
            {
            hid_word[i]='*';printf("%c", hid_word[i]);
            }
        printf("\n");
 
    for(int j=1; j<30;j++)
    {
        char letter;
        int letterinword=0;
        printf("Enter the letter: ");
        scanf(" %c", &letter);

        for (int i=0;i<l;i++)
            {
            if (letter==word[i])
                {
                hid_word[i]=letter;
                letterinword=1;
                }
            }
        if (letterinword==0) printf("This letter is not a part of the word!\n");
        
        
        for (int i=0;i<l;i++)
                {
                printf("%c", hid_word[i]);
                }
        printf("\n");
                    
        int match=0; int count=0;
            
        for (int i=0;i<l;i++)
            {
                if(word[i]==hid_word[i]) match=1;
                else match=0;
                count=count+match;
            }
            printf("\n");
            if (count==l) { printf("Congratulations, you guessed the word!"); break;}
    }
       return 0;
}
This is the user's console:


Code:
Enter the word: thanksskaa
Press any key to hide the word!
...
Code:
**********
Enter the letter: a
**a*****aa

Enter the letter: b
This letter is not a part of the word!
**a*****aa

Enter the letter: e
This letter is not a part of the word!
**a*****aa

Enter the letter: o
This letter is not a part of the word!
**a*****aa

Enter the letter: t
t*a*****aa

Enter the letter: s
t*a**ss*aa

Enter the letter: h
tha**ss*aa

Enter the letter: k
tha*ksskaa

Enter the letter: n
thanksskaa

Congratulations, you guessed the word!
srecko is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
basic, characters, problem



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic % Problem? SammyG Elementary Math 3 November 14th, 2015 08:20 PM
Dirichlet characters MrBibbles Number Theory 4 July 25th, 2013 12:07 PM
Special characters mathbalarka New Users 2 May 30th, 2012 09:23 AM
basic problem balibalo Advanced Statistics 2 February 11th, 2008 05:37 AM
A Basic Problem STV Economics 16 November 20th, 2007 12:58 PM





Copyright © 2017 My Math Forum. All rights reserved.