Saturday, May 22, 2010

C++ program?

I have to make the following program (array-related) in turbo C++, but I'm having trouble!





Store "n" names (the exit option is when the name "mary" is typed or when it reaches 10 names) in an array, and the longitude of each name in another array. The content of the arrays must be printed backwards.





what i did is:





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


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


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





void main()


{


char array[10][15], name[15];


int x, band=0, arlong[10], y=0;





clrscr();





for(x=0; x%26lt;10%26amp;%26amp;band!=1; x++)


{


printf("\nWrite name to store: ");


scanf("%s", name);


if(strcmpi(name, "mary")==0)


band=1;





array[x]=name;


arlong[y]=strlen(name);


y++;


}





printf("Now printing the contents of the name array backwards:\n");


for(x=10;x%26gt;0;x--)


printf("\n%s", array[x]);





printf("\nNow printing the content of the longitude array backwards:\n");


for(y=10;y%26gt;0;y--)


printf("\n%d",arlong[y]);





getch();

















}





But I can't get it to work!! Any suggestions?

C++ program?
//Critique of program follows -


// Comments in /* */





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


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


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





void main()


{


char array[10][80], name[80];


/*Change to 80 - store more characters*/


int x, band=0, arlong[10], y=0;





//clrscr(); /*Dont use this unless you have to - */





for(x=0; x%26lt;10; x++) //Just use break


{


printf("\nWrite name to store: ");


scanf("%78s", name); /*Keep that length from blowing the buffer*/





if(strcmpi(name, "mary")==0) break; /*See how easy?*/





/* array[x]=name; This doesn't do what you think it does... try strcpy or memcpy */


strcpy(array[x],name);


/* You cant copy the whole string using the = sign */





arlong[y]=strlen(name);


y++;


//If you want it to print mary too, put the if %26amp; break after the y++;


}





printf("Now printing the contents of the name array backwards:\n");


/*Y contains how many items we entered*/


for(x=y-1;x%26gt;=0;x--) /*changed to 9-%26gt;0 */


printf("\n%s", array[x]);





printf("\nNow printing the content of the longitude array backwards:\n");


for(x=y-1;x%26gt;=0;x--)


printf("\n%d",arlong[x]);





printf("\n");


return ;


}





/* As a side note, using x %26amp; y in this is confusing. If you are going to iterate (for loop) i is suficient most of the time. Just keep using i (instead of x).





But incrementing y for each record isn't obvious. You should call it maxrecords or count; something obvious..





This is also C code, not c++ code.
Reply:strcpy - not strcmpy Report It

Reply:First off, you aren't using the C++ standards. Unless this is an assignment, I suggest you follow the standards, which means using the 'std' namespace (no '.h' at the end of "#includes", etc.).





Second, when printing the contents of the array in reverse, you have to remember that C++ uses zero-based indexing. The first item in the array is at index '0'. 'for (y=10; y %26gt; 0; y++)' will start at index '10' (which doesn't exist due to zero-based indexing), and it will only print to index '1' ('y %26gt; 0'). You should try 'y %26gt;= 0'. And you need to change '10' to another number.





I won't give you a flat-out answer because this seems to be an assignment, and I will not give answers away. I hope this helps. =)


No comments:

Post a Comment