Error in C++ code(Help Required)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Error in C++ code(Help Required)

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Error in C++ code(Help Required)

    5: /*
    6: It has numerous flaws.
    7: */
    8:
    9: #include <stdio.h>
    10: #include <stdlib.h>
    11: #include <string.h>
    12:
    13: #define MAXWDLNTH 20
    14: #define MAXWDCNT 200
    15: #define MAXLINELNTH 100
    16:
    17: /****************************************************************
    18: readwords - read blank-separated words from stdin
    19: ***************************************************************/
    20: int readwords(char words[][MAXWDLNTH]) {
    21: int i;
    22: char line[MAXLINELNTH];
    23: char* word;
    24:
    25: i = 0;
    26: while (gets(line) != NULL) {
    27: line[strlen(line)] = '\0'; /* trim off newline character */
    28: for (word = strtok(line, " ");
    29: word != NULL;
    30: word = strtok(NULL, " ")) {
    31: strcpy(words[i], word);
    32: i++;
    33: }
    34: }
    35: return(i);
    36: }
    37:
    38: /****************************************************************
    39: swap - swap 2 words
    40: ***************************************************************/
    41: void swap(char words[][MAXWDLNTH], int i, int j) {
    42: char temp[MAXWDLNTH];
    43:
    44: strncpy(temp, words[i], MAXWDLNTH);
    45: strncpy(words[j], words[i], MAXWDLNTH);
    46: strncpy(words[j], temp, MAXWDLNTH);
    47: }
    48: 49: /****************************************************************
    50: sortwords - sort the array of words
    51: ***************************************************************/
    52: void sortwords(char words[][MAXWDLNTH], int wordcount) {
    53: int i;
    54: int j;
    55:
    56: for (i = 2; i < wordcount; i++) {
    57: for (j = 1; j < i; j++) {
    58: /* If words[i] < words[j] then swap them */
    59: if (strcmp(words[i], words[j]) < 0) {
    60: swap(words, i, j);
    61: }
    62: }
    63: }
    64: }
    65:
    66: /****************************************************************
    67: printwords - print the array of words
    68: ***************************************************************/
    69: void printwords(char words[][MAXWDLNTH], int wordcount) {
    70: int i;
    71: int j;
    72: int wdlnth;
    73:
    74: for (i = 1; i < wordcount; i++) {
    75: wdlnth = strlen(words[i]);
    76: printf(words[i]);
    77: printf("\n");
    78: }
    79: }
    80:
    81: /****************************************************************
    82: main program - no command line arguments
    83: ***************************************************************/
    84: int main() {
    85: /* char words[MAXWDCNT][MAXWDLNTH]; */
    86: char* words;
    87: int wordcount; /* number of words to sort and print */
    88:
    89: words = malloc(MAXWDCNT*MAXWDLNTH);
    90: /* Read the words */
    91: wordcount = readwords(words);
    92: /* Sort the words */
    93: sortwords(words, wordcount);
    94: /* Print the words */
    95: printwords(words, wordcount);
    96:
    97: return(0);
    98: }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Error in C++ code(Help Required)

    Please format your code correctly and use [code] tags when posting code (Go Advanced, select code and click '#'. Do not use line numbers with the code and indent appropriately for readability.

    What flaws? Does it compile? What should the code do and what does it do? Have you tried debugging the code? As this is c++ code, why are you using the c library for input/output and string manipulation rather than the c++ Standard Template Library (STL)?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center