| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
i have two integer arrays, temp_guess1] and guess[4], with temp_guess[1] being a four digit number, and i want to parse the four digit number from temp_guess[1] into guess[1], [2], [3], and [4]. i tried strcpy (guess, temp_guess[1]); but when i compile it i get an error which says "error C2664: 'strcpy' : cannot convert parameter 1 from 'int [4]' to 'char *'". at first i thought it meant that it couldnt convert an int to a char but both of my arrays are int. im not really familiar with the strcpy function, am i doing something wrong? i hope i made my problem clear. if anybody can help, it will be appreciated.
thanks |
|
#2
|
|||
|
|||
|
Hey Maxmax. The problem is that strcpy works for char* and char[]. Yours is an Int[]. You should use the function memcpy which copies contents from memory cells, independently of their content type.
Good Luck Anibal. |
|
#3
|
|||
|
|||
|
hey
heres what i tried: cout << endl; cout << "::Numbers::\n\nDeduce my four digit number with the fewest guesses possible.\nMy number has no repeating digits.\n\nEnter your guess: "; cin >> temp_guess[1]; void *memcpy(void *guess, const void *temp_guess[1], size_t N); cout << temp_guess[1] << endl; cout << guess[0] << guess[1] << guess[2] << guess[3] << endl; when memcpy is finished its job it displays whatever 4 digit number you entered with another 3 zeros, and then 'guess' doesnt seem to have anything in it. i need to have all 4 digits of 'temp_guess', that the user enters, parsed into the four slots in 'guess[4]'. am i doing something wrong? anywyas thanks alot for the help, this is the first time ive gotten anywhere on this project in like 3 weeks. keep it comin. -max |
|
#4
|
|||
|
|||
|
Hold it...let me see: so the user inputs a four digit number and you want to insert it into a four items array? for example:
user: 1234 arrary[0] = 1, array[1] = 2, array[2] = 3 and array[3] = 4 is that it? If so, I suggest you divide the number and assign it directly like this: remaining = <userinput> for (i = 0; i < 3; i++){ array[i] = remainin / (10 ** (3 - i)) remainin = remaining % (10 ** (3 - i)) } array[3] = remaining I'm not sure wheather this is what you're looking for...if not, write back and We'll keep trying Anibal. |
|
#5
|
|||
|
|||
|
Hey, yeah thats exactly what im trying to do, i need the users input seperated into 4 slots with only one digit each in an array exactly like you said, becasue im going to compare the inputed values with another 4 digits further along in the program. So far ive just been running on trial and error for this part of the program, im extremely new to C++ as you can see, and i dont quite grasp how to implement:
remaining = <userinput> for (i = 0; i < 3; i++){ array[i] = remainin / (10 ** (3 - i)) remainin = remaining % (10 ** (3 - i)) } array[3] = remaining into the program. I tried it in a few different ways but no luck yet. If you could explain of what you did there that would be awesome, im pretty sure were both on the same page about my problem now. Anyways sorry for being so much trouble and thanks again for your help so far. - max |
|
#6
|
|||
|
|||
|
Hey! the code is quite simple. It's pure C (no c++) so it should work. The thing is like this:
First, declare the variables input, remaining and i We Have BOB here to play...so he inputs a number. You get that number with scanf("%d",&input); Then you assign the variable: remaining = input; the next par of code is to run like this! (array is the four sized array in which you save the input digit by digit. if you have named it inputArray, simply replace the name) for (i = 0; i < 3; i++){ array[i] = remaining / (10 ** (3 - i)); remainin = remaining % (10 ** (3 - i)); } array[3] = remaining; <-- here you have your array in four separated numbers the user has entered, so you do whatever you want to do with it. I forgot to place the ; at the end of each sentence before. Don't forget to place it yourself. It's that simple....anyway..if you still don't get it, give me your mail and we'll keep this up later. Anibal. |
|
#7
|
|||
|
|||
|
hey
OK heres what i did, first i declared: int input; int remaining; int p; int input_array[4]; Then i put: cout << endl; cout << "::Numbers::\n\nDeduce my four digit number with the fewest guesses possible.\nMy number has no repeating digits.\n\nEnter your guess: "; scanf("%d",&input); remaining = input; for (p = 0; p < 3; p++) { input_array[p] = remaining / (10 * (3 - p)); remaining = remaining % (10 * (3 - p)); } input_array[3] = remaining; cout << input_array[0] << input_array[1] << input_array[2] << input_array[3] << endl; Everything was going well until i executed and tested it out. i entered for example 3947, and for some reason the computer displayed 131017 for input_array. did i do something wrong? max |
|
#8
|
|||
|
|||
|
Here's the complete code in C. I've tested and it works. The other one requiered some changes, but since you're new to C, you couldn't know that.
Good Luck! Anibal. #include <stdio.h> #include <stdlib.h> #include <math.h> #include <complex.h> void main() { int input; int remaining; int p; int input_array[4]; printf("Enter a four digit number please: "); scanf("%d",&input); remaining = input; for (p = 0; p < 3; p++) { input_array[p] = remaining / pow(10,(3 - p)); remaining = remaining % (int) pow(10,(3 - p)); } input_array[3] = remaining; printf("array: %d-%d-%d-%d",input_array[0],input_array[1],input_array[2],input_array[3]); scanf("%d",&input); } |
|
#9
|
|||
|
|||
|
It works!!! Thanks alot man, its been a really long time since ive gotten anywhere on this project. I hope sometime ill be able to help you out but something tells me thats not gona happen for a while due to my skills (or lack thereof) in C++ hahahah. Thanks again for all the help, ill cya around.
max |
|
#10
|
|||
|
|||
|
I'm happy about it! And don't be so sure about your lack of skills. Not everything in life is restricted to C++. Just try to help others like I helped you and we'll all be allright then.
Good luck with the proyect! Anibal. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > need help with parsing arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|