Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Desperate need of C++ help...


  • Please log in to reply

#1
DarkFable

DarkFable

    New Member

  • Member
  • Pip
  • 7 posts
Ok, i have tried a few other sites, but have yet to get a response and i'm getting really nervous...
Tomorrow I have a C++ test in my grade 11 computer science course and my teacher gave us a few problems to do/practise at home. (Note we use Visual C++ from Visual Studio 6.0)

Problem 1. Have the user input a 4-digit character array or string. Have your program convert this string into an integer value without using the atoi function.

As it clearly states, i cannot use the atoi function. Now a previous question wanted the opposite (integer to char array) with itoa function and i got that working with the code i have in the txt file.

Problem 2. Have the user guess at random number. Have the computer tell the user either “Higher” or “Lower” until correct. The program will show the total number of guesses. Also, if the user is a moron, tell them so. For example, assume the correct number is 12. If the user inputs 7 and your program informs the user “higher”, if at any later time the user inputs a number of 7 or lower, the program is permitted to call the user a “moron”.


My question is if someone can please help me by coding this, just so that i can review the code and be ready for the test tomorrow. I've missed the class the last 4 days with the flu and have fallin a little behind. Also, if you could code it at a similar level as the code in the txt file, i would really appriciate it because thats how far our class as gotten so far. Please refer to the text file.

PS: I know it may not be exactly whta you'd want to do, but i would really appriciate it and it would help me dramatically.

Attached Files


  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
DarkFable,

The problem here is not the actual code, but rather that you understand why the code works and what it does. Remember that internally, C just represents characters as numbers --- and so strings of characters are just strings of numbers. So sample code for your first question might be (in C):

int
string_to_int(char *string)
{

int i;
int result = 0;

for(i = 0; i < 4; i++) {
result *= 10;
result += (string[i] - '0');
}

return result;

}


Now --- more important --- why does it work? You are given a four-character string of numbers. (Let's assume you know char *string is already allocated, and has 4 characters, so we don't have to check for bad data or buffer overruns, which we normally would).

Then, you parse the four characters of the string (numbered 0 to 3), from the LEFT, since this is how strings are parsed. So, for each digit of the string, you must take the previous total, multiply by 10, and add the current digit's numeric value. For example: suppose you had the number 1234. You would parse from the left, beginning with 1. So the value of the result variable would be 1, then 1*10+2, then (1*10+2)*10+3, then ((1*10+2)*10+3)*10+4, which equals the number 1234. See?

The reason to add the ( - '0') part is that C treats characters as being represented by their ASCII values, NOT their actual numeric values. The numeric values may be found by taking the difference of the character's ASCII value with 0's ASCII value, so you are finding the value relative to zero, which is the numeric value you want. (Again we assume that all the data is correctly numeric; we should actually be checking for non-numeric characters to be careful.)

Does this help? :tazz:

Edited by Swandog46, 28 October 2005 - 03:16 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP