Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:50:54+00:00 2026-06-02T10:50:54+00:00

I am trying to write a programme in C for an assignment that detects

  • 0

I am trying to write a programme in C for an assignment that detects CSV format eg. decimalcommadecimal. and gives a output as to if the file is in the required format or not. I have tried using various inputs from stdin and using isdigit etc. but to no success. I’m a mega noob and have barely done any C programming before, I attempted to use regexc but couldn’t figure out the syntax for using it.

#include <ctype.h>
#include <stdio.h>
const char EOL = '\n';

int cbreak(void);
int check_dig(void);
int value =1;
char c;

int main()
{

    while((scanf("%c" ,&c)) !=EOF&& value !=0)
    check_dig();

    printf("\n%d\n",value);
}

int check_dig()
{
    if (c == EOL)
        scanf("%c", &c);
    if (c == isdigit)
        scanf("%c", &c);
    else if (c == ',')
        scanf("%c", &c);
    else value = 0;
}

Thanks Guys I’m now to this stage but stumped as how to finish, I need to printf either 1 or 0 depending on validation and I want to do this as suggested using a return value.

#include <ctype.h>
#include <stdio.h>

int check_digit(int);
int check_comma(int);
int skip_char(int);
int main()
{
    int c;

    while ((c = getchar()) !=EOF)
        if (check_digit(c))
            skip_char(c);
        else if (check_comma(c))
            skip_char(c);
        else return 0;
}

int check_digit(int c)
{   
    if (isdigit(c))
        return 1;
    else return 0;
}    

int check_comma(int c)
{
    if (c == ',')
        return 1;
    else return 0;
}

int skip_char(int c)
{
    c = getchar(); // will this skip 2 chars as i have a while loop that has c=getchar()??
    return c;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:50:56+00:00Added an answer on June 2, 2026 at 10:50 am

    First off I’d recommend to not use that vast list of globals. Use parameters in functions and return values from functions.

    Second isdigit does not work like that. It takes a parameter and return true or false. isdigit

    Further I’d use getchar over scanf.

    Your int check_dig() function is a bit strange. You keep reading characters inside the function.

    I would perhaps do something like:

    int valid_csv(char c)
    {
        if (isdigit(c))
            return 1;
        /* etc. other checks */
        if all fails
        return 0;
    }
    
    int main(void)
    {
        int c;
    
        /* read chars into c and call fun by something like */
        valid_svc(c);
    
        return 0;
    }
    

    Edit: As a rule of thumb. A function should do one thing, and one thing only, and do it well. The name of the function should reflect what it does.


    Edit.2:

    You do not need to “skip char”. The way it goes, in your new code, is that you skip every other character.

    I.e.:

    File: 12,33,66,14

    In your code you’ll get

    • c = getchar => c == 1
    • c is digit
      • getchar => you read 2 (and never validates it)
    • c = getchar => c == ,
    • c is comma
      • getchar => you read 3 (and never validates it)
    • …

    Further; I know I wrote “a function should do one thing” – but but not that literal. Ie your new check_digit is redundant. Use isdigit directly. If you have floats in your csv you’ll have to expand or use a different approach.

    To illustrate by example; easier then writing on here 🙂

    #include <ctype.h>
    #include <stdio.h>
    
    int valid_csv_chr(int);
    int valid_csv(); /* guess naming could be better. */
    
    int main(void)
    {
        if (valid_csv())
            puts("1");
        else
            puts("0");
    
        return 0; /* Main should return 0 if there was no "crash" scenario etc.
         * You could also return i.e. 1 if the file is not validated as csv. 
         * Do not think of 1 and 0 as boolean true / false here. */
    }
    
    int valid_csv()
    {
        int c;
    
        while((c = getchar()) !=EOF) {
            if (!valid_csv_chr(c)) {
                return 0;
            }
        }
    
        return 1;
    }
    
    int valid_csv_chr(int c)
    {
        if (isdigit(c))
            return 1;
        if (c == ',')
            return 1;
        if (c == '\n')
            return 1;
            /* add checks for space etc. */
        return 0;
    }
    

    Edit.3:

    Code structure is something one has to learn just as much as the language itself. It is learning by doing and writing. That one realize that re-structure of code is necessary happens from time to time – but the more one think before writing, make a simple structure at first and expand it, etc. it is more avoidable.

    Anyhow; practice, practice, practice. And keep these topics in mind at all time.

    Even if it can “look” simple, it is not. I think often books, tutorials, courses etc. have all to little focus on this topic. It is all about for, if, functions etc. and all to little on how to stitch it all together in a good way.

    There are several advantages of splitting up the code.

    • It makes it much more readable.
    • It makes it easier to maintain.
    • Bugs and errors can often be fixed by fixing a small function rather then a huge one.
      • On some cases I have seen code of thousands of lines where one have some monster functions that is buggy. Fixing by tweaking it is close to impossible and a complete re-write is only option.
    • It is easier to optimize a function that does one task, and is not to big.
    • It is easier to expand to cover more scenarios when using smaller functions.
      • Say for example in your program. You could change it to “validate data file” covering csv, tab delimited, aligned, etc.

    The last point is actually a way I often think of it when writing; “How should I best implement this code so that, if, in the future, I’d like to expand it to cover more scenarios, it can be easily done.”

    Myself I use this as a base when writing in C combined with things I’ve learned in K & R’s ANSI C book ++. See for example what is written about functions.

    Also; being strict on coding style makes it much easier to read and maintain. I use in large part what is described in the document above. It is no law, but being conscious about it makes the coding life so much simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that reads text from external file (string string
I'm trying to write a program to process the BSD-style process accounting file under
I'm trying to write a program that uses sockets to connect with other instances
I'm trying to write a program in C (on Linux) that loops until the
I am trying to write a program that allows a binary to be run,
I am trying to write a program that displays the integers between 1 and
I'm trying to write a program in Java that uses osql to generate a
i am trying to write a program that close explorer then runs another program.
I am not an experienced Java programmer and i'm trying to write some text
I am a programmer, new to jsp, trying to write some code. i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.