C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 17th, 2005, 01:51 PM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
Exclamation need help with parsing arrays

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

Reply With Quote
  #2  
Old January 17th, 2005, 02:02 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
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.

Reply With Quote
  #3  
Old January 18th, 2005, 02:49 AM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old January 19th, 2005, 06:19 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 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.

Reply With Quote
  #5  
Old January 20th, 2005, 06:10 PM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old January 21st, 2005, 02:22 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
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.

Reply With Quote
  #7  
Old January 21st, 2005, 03:39 PM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
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

Reply With Quote
  #8  
Old January 21st, 2005, 05:07 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
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);
}

Reply With Quote
  #9  
Old January 22nd, 2005, 01:19 AM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
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

Reply With Quote
  #10  
Old January 23rd, 2005, 03:15 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > need help with parsing arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT