Thread: Update on previous post; new problem

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    19

    Update on previous post; new problem

    Hi all,

    I've come a little farther in my C studies since my last post, and I decided to try my hand at building a custom function. The code is compiling, but the counters no longer count past 1. I cannot for the life of me figure out the problem. Here is my updated code:

    Code:
    #include<stdio.h>
    #include <ctype.h>
    
    void count(int counter, char symbol) {
      counter++;
      for (int i = 0; i < counter; i++) {
        putchar(symbol);
      }
      puts("\nPlease enter a letter: ");
    }
    
    int main(void) {
      char c;
      int vowelCount = 0, conCount = 0;
      puts("\nPlease enter a letter: ");
      do {
        if (isalpha(c = getchar()) || c == '0') {
          switch (tolower(c)) {
            case '0':
              puts("\nThank you for counting on me.");
              break;
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
              count(vowelCount, '*');
              break;
            default:
              count(conCount, '!');
          }
        }
        else if (!isspace(c)) {
          puts("\nAny character not in the English alphabet is not an accepted input for this application.\n\nPlease enter a letter: ");
        }
      } while (c != '0');
      return 0;
    
    }
    What it should do:

    Code:
    >>>c
    !
    >>>u
    *
    >>>l
    !!
    >>>8
    Any character not in the English alphabet is not an accepted input for this application.
    >>>r
    !!!
    What it does:

    Code:
    >>>c
    !
    >>>u
    *
    >>>l
    !
    >>>8
    Any character not in the English alphabet is not an accepted input for this application.
    >>>r
    !
    Any help appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to return the new value of the counter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    19
    Hi Salem,

    Returning the counter (and changing void to int accordingly) did not fix the problem. I'm not sure what I'm supposed to do with the returned value anyway.

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    19
    Actually, I spoke too soon. I just had to store the result of calling the function back into the appropriate counter variable. Thank you!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I understand that you come from a Python background, so you might be aware that functions should each do one thing and do it well (the single-responsibility principle). Your count function seems to be trying to do three rather different things at once: increment a counter, print a bar, and prompt for more input. It would likely make more sense to have three different functions: a function to determine the category of a letter and increment the corresponding counter; a function that prompts for input, reads it, validates it, and returns valid input for further processing; a function for printing a bar given a count and a symbol.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2020
    Posts
    19
    Hi, laserlight! Unfortunately, my programming background is very surface level, and until now I've never really cared about what is good practice and what is not. Now that I'm starting a CS degree, this is the kind of stuff to which I definitely need to pay more attention, so thank you for educating me a little bit! Your explanation of the single responsibility principle makes perfect sense to me and I will definitely keep it in mind for future projects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-26-2012, 09:32 PM
  2. Update to a problem
    By ru11 in forum C Programming
    Replies: 7
    Last Post: 11-10-2011, 11:51 PM
  3. Replies: 4
    Last Post: 07-16-2011, 06:08 PM
  4. post update
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-12-2001, 10:47 AM

Tags for this Thread