Problems with code

This is a discussion on Problems with code within the C++ Programming forums, part of the General Programming Boards category; Here the snippet: Code: #include <iostream.h> #include <stdlib.h> #include <conio.h> char ch; //Holds current input for evaluation. int a; //Holds ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    18

    Problems with code

    Here the snippet:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    char ch; //Holds current input for evaluation.
    int a; //Holds number of chain starts.
    int b ; //Holds number of chain ends.
    int sum ; //Sum of chains.
    
    void main()
    {     
          a = 0;
          b = 0;
          sum = 0;
          While(ch != '.')
          {
                   ch = getch();
                   if (ch == 'a')
                      a++;
                   if (ch == 'b')
                      b++;
                   if (ch == 'a' && b > 0)
                   {
                      sum = sum + (a*b);
                      b = 0;
                   }
                   if (ch == '.' && ch == 'c')
                   {  
                      sum = sum + (a*b);
                      a = 0;
                      b = 0;
                   }
                   cout << "\n << sum;
          }
          system("PAUSE");
    }
    As you can see, the program looks through a string as it is being typed and output the number sub-series in the string that start with 'a' end with 'b' and have no 'c' in the middle. So the string "dsacbsabxbxxa" would return 2 (for "ab" and "abxb").
    This code won't compile in Dev-C++ with multiplile errors.
    What's the problem?

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Saw a typo - while shouldn't be capitalized....

    What errors are you getting?

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    your while loop should not be capitalized.
    and you're missing a " after the \n in your last cout statement to start with.

    --system() calls are not always the best way to either.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    18
    Fixed the while capitalization thingy, thanks.
    I get only one error now (instead of seven): "implicit decleration of function "int getchar()".

    Whatever that means. I added cout only in this post, actually , isn't in yet.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    18
    Thanks! It compiles now. It still doesn't work though. And I had some glaringly obvious bugs in the first version. But I've only been coding for a couple of months and that only in school...
    Please stick around, if I won't be able to figure this out I may need your help. BTW, latest version of code is:
    EDIT: That code was still awfuly buggy. Here's the final algorithm. Still could use some tweaking, I know, but it works. I'm so full of myself it's sad.
    Code:
    /*Final version with a (finally) working algorithm*/
    #include <iostream.h>
    #include <conio_mingw.h>
    
    char ch; //Holds current input for evaluation.
    int a; //Holds number of chain starts.
    int b ; //Holds number of chain ends.
    int sum ; //Sum of chains.
    
    void main()
    {     
          a = 0;
          b = 0;
          sum = 0;
          while(ch != '.')
          {
                   ch = getch();
                   if (ch == 'a' && b > 0 && a > 0)
                   {
                      sum += (a*b);
                      b = 0;
                   }
                   else if (ch == 'a')
                      a++;
                   else if (ch == 'b' && a > 0)
                      b++;
                   
                   else if (ch == '.' || ch == 'c')
                   {  
                      sum += (a*b);
                      a = 0;
                      b = 0;
                   }
                   putch(ch);
                   cout << sum; //Used for debugging.
           
          }
          cout << "\n" << sum;
          getch();
    }
    Last edited by dasher; 01-23-2002 at 11:00 AM.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > I'm so full of myself it's sad.



    One last thing - in the future - remember that main returns an int...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 01:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21