Thread: Need some help

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Need some help

    I am trying to create a program to score a bowling game. I have a variable "B" that is defined as an int. There is another varaible "K" that is defined as an int.

    I have used B = getchar() to get the value of B and i then do
    K = B + K
    and assign the value of K to a point in my array, and print the array to the screen, but K is returned on the monitor as a funny looking character. How can i get K to be returned to the monitor as a number and not this strange character?
    Last edited by Mike56p; 04-25-2005 at 09:12 AM. Reason: Poorly phrased question

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    What do you mean by printing integers to a string?

    Post some code and sample output.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Like joshdick said, some code would really help.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If what you're looking to do is convert a string like "213" into the int 213, use atoi(). There are similar functions for longs and floats, atol() and atof(), respectively.

    If you just need to convert a single character, there's a simple way to do that by exploiting the relationship between a character and its ASCII number.

    For example, if you have:
    Code:
    char ch = '7';
    
    int x = ch - '0';
    Now x == 7. Furthermore, that code works for any number 0-9 stored in ch. That's basically what atoi() is probably doing.
    Last edited by joshdick; 04-25-2005 at 09:35 AM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Thanks that fixed it!

Popular pages Recent additions subscribe to a feed