English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm having some trouble with this when it seems as though it shouldn't be hard at all. Yet everytime I try to compile, I get tons of errors and I don't understand why. The code is supposed to read in a pattern of 30 decimal digits into an array of short integers. I need it to read the input stream, report the number of elements read, and then output the number. It should also skip any leading invalid characters and should only read 30 digits. Anything after the first 30 digits should be ignored (using a function from cctype library).
Here's the code I have .. so if anyone could tell me what I'm doing wrong it would be entirely helpful. I'm thinking maybe it's somewhere along the lines of the problem with converting the numbers to an array of short ints?

#include
#include
#include
using namespace std;

const int maxLen=30;
int main()
{
int i;
short pattern[maxLen];
int isdigit(i);
for (i=0;i cin>>setw(31)>>pattern;
return 0;
}

2007-10-06 19:26:09 · 3 answers · asked by volleyballdawl 2 in Computers & Internet Programming & Design

I changed the coding around so I didn't have to use iomanip library. Would this work instead?

int i, maxLen = 30;
short pattern[maxLen];

for (i=0; i cin.ignore(maxLen, '\n');

return 0;

2007-10-06 19:51:16 · update #1

Sorry.. each one acts as an individual value. So for example:

12345 would take up 5 values .. not just one single value.

2007-10-06 20:33:20 · update #2

3 answers

try this code.. i've written this in c.. for c++ change the printf and scanf to cout and cin.

int main()
{
int i;
short pattern[30];
for (i=0;i<30;i++)
{scanf("%d",&pattern[i]);
if(isdigit(pattern[i]==0)
{ printf("invalid number"); exit(0); }
}
for(i=0;i<30;i++)
printf("%d \t",pattern[i]);
return 0;
}

2007-10-06 20:01:33 · answer #1 · answered by sim 4 · 0 0

Please note that Sim's solution won't work properly, as after he uses 'scanf("%d%") to read into pattern, the data there is a *binary* value already, so 'isdigit()' will totally fail (as that expect a *character* value).

Also, there's a specification error in there - is it supposed to read 30 *numbers* , or 30 *digits*? In other words, is '12345' *one* value, or is it *5* values? (If you're supposed to read in 30 *digits*, I'd suggest a *character* array, and either an 'isdigit() test or something similar..... And are you supposed to stop if the person has entered 15 2 digit numbers, or 3 10-digit, or so on? (bonus points for noticing that a 'short' can't hold more than 32767 or so, so even most 5-digit numbers won't fit, and 6-digit and higher are a total loss... ;)

2007-10-07 03:29:46 · answer #2 · answered by Valdis K 6 · 0 0

To put the input into an array you want to code this
cin>>setw(31)>>pattern[i];

2007-10-07 02:42:33 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers