Thursday, July 9, 2009

Simple C++ Array Question?

I have a question regarding having a user inputting data into an integer array.





I need a user to enter a series of 0's and 1's (don't worry, i have the defensive programming set up to make sure they don't enter anything else). I need that data to be placed into an array called 'data'.





The way it's set up now, I ask for 10 pieces of data, and the user enters them one at a time and hits enter in between every one. It then stops asking and going through the loop because counter %26gt; 10. I don't want 10 to be hardcoded, and I don't want thte user to have to enter them one at a time.





CIN%26gt;%26gt; takes one character at a time, what code would take them all? Also, I need every bit to be put in a different index. For instance, if the entry is 101, I don't want 101 in position 0, i want 1 in position 0, 0 in position 1, and then 1 at position 2.





Can anyone help?

Simple C++ Array Question?
you could try to read as many words as the user gives you, and whenever the user prints enter, and cin%26gt;%26gt; returns, you can parse the string.





int received=0;


// repeat till we have 10 items


while (received%26lt;10) {


cin %26gt;%26gt; word;


// now parse the word manually - this is a bit of work


// then add the number of valid "pieces of data" to "received"


// i.e. if the user enters "1": fill your array with a "1" and add 1 to received (received+=1);


// if the user enters "101": fill your array with "1", "0", "1", and add 3 to received (received+=3);


}
Reply:// HELP.cpp : Defines the entry point for the console application.





#include %26lt;conio.h%26gt;


#include %26lt;iostream%26gt;





using namespace std;





#define ESC 27


#define ENTER 13





//------------------------------------


//


// binary_gets()


//


// get user input into buffer...


//------------------------------------


int binary_gets(char *buff, int max)


{


char ch=0;


int iii=0, done = 0;





while(iii %26lt; max %26amp;%26amp; !done )


{


ch=getch();





switch(ch)


{





//test for 'One' or 'Zero' ...the only characters permitted


case '0':


case '1':


putch(ch); //display OK





//Note, this code can be modified to store


//as integers by subtracting '0' from 'ch' right


//here ( pls see usage in main() )


buff[iii++] = ch; //store it


break;





case ESC: // user wants out


iii=0; //cancel all





//fall thru....





case ENTER: //user signaled done, keep all


done = 1;


break;





default:


break;


}





}





buff[iii] = '\0'; //cap it or kill it, depending iii


return (iii %26gt; 0); //return True if any data captured





}





int main()


{


char buff[20];


int iii, pow, num;





cout %26lt;%26lt; "Enter your binary number: ";


if(binary_gets(buff, 10))


{


cout %26lt;%26lt; endl %26lt;%26lt; "Seems you entered: " %26lt;%26lt; buff %26lt;%26lt; endl;





///Now, convert and display in decimal


iii=strlen(buff);


pow = 1;


num = 0;


while(iii--)


{





// Next: we say [ buff[iii] - '0' ] because this is


// character array in a numeric application...


num += (pow * (buff[iii] - '0' ));


pow *= 2;


}


cout %26lt;%26lt; "In decimal, that's " %26lt;%26lt; num %26lt;%26lt; endl;


}


else


cout %26lt;%26lt; endl %26lt;%26lt; "User quit early" %26lt;%26lt; endl;





return 0;


}
Reply:hmm... a tricky workaround, but try this:


create a string variable, then use [scanf("%s",%26lt;varname%26gt;)] or similar to get the user input. Then cast the string into ints, implementing a do-while loop to ensure that the "string" was actually 1's and 0's. before casting the string to int, get the separate "characters" from within the string, then cast the individual characters to ints to be used in the array. This seems like an excessive workaround(and be prepared for compiler errors), but try it, then fine tune it if need be.





EDIT: call the size method of String itself to set whatever size limits you want, placing them into the do-while "verification" loop, or you could set the size limit on the post-cast chars or ints.

pansy

No comments:

Post a Comment