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 8897655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:24:09+00:00 2026-06-15T00:24:09+00:00

I have been trying to work with C++ console app on linux. I want

  • 0

I have been trying to work with C++ console app on linux.

I want to change the options of the user depending on what parameter he used.

Like: ./my -n 1234 or ./my -f file.txt

-n or -f is always argv[1]
1234 or file.txt is always argv[2]

my main()

int main(int argc, char *argv[])

Here is my code so far:

string numlist, num;


if (argc < 3) {
   fprintf(stderr,"usage stuff...%s %s", argv[0], argv[0]);
   exit(0);
}

char *f = "-f";
char *n = "-n";
char *argv0 = argv[0];
char *argv1 = argv[1];
char *argv2 = argv[2];

if (strcmp(argv1, f) == 0){
    numlist = argv[2];
    //more data for this parameter
}

if (strcmp(argv1, n) == 0){
    num = argv[2];
    //more data for this parameter
}

Problem:

when i try ./my -n 1234
This should skip 1st if block, then execute 2nd if block.
or ./my -f file.txt
Should execute the 1st if block, and skip the 2nd if block.

Now with my current code if i enter either -n or -f it skips all if blocks.

— edit —
I guess I fixed it the problem was actually not on the argv but on my if blocks.

Thanks everyone I’ll check all your suggestions.

  • 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-15T00:24:10+00:00Added an answer on June 15, 2026 at 12:24 am

    I don’t know what’s your complete source code but here is what it worked for me:

    #include <string.h>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main(int argc, char **argv){
    
        if (argc < 3) {
           fprintf(stderr,"usage stuff...%s %s", argv[0], argv[0]);
           exit(0);
        }
    
        string numlist;
        string num;
    
        char *f = "-f";
        char *n = "-n";
        char *argv0 = argv[0];
        char *argv1 = argv[1];
        char *argv2 = argv[2];
    
    
        if (strcmp(argv1, f) == 0){
            numlist = argv[2];
           //more data for this parameter
        }
    
        if (strcmp(argv1, n) == 0){
            num = argv[2];
        //more data for this parameter
        }
    }
    

    I’ve added the using namespace std to make the writing of the code easier, also i’ve moved the validation to the top, because if argv[2] doesn’t exists, it will throw an error.

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

Sidebar

Related Questions

I have been trying to work with the Projector and Ray classes in order
I have been trying to make it work with either fwrite or fputcsv but
I have been trying to get nServiceBus to work with Ninject 2.0 as the
I have been trying to get this to work correctly and I think I
I have been trying to get this jsfiddle to work. So far without luck.
I have been trying to get Twitter Bootstrap btn-group with dropdown to work for
This is crazy, I have been trying for hours to get this to work.
Okay I been trying to work this out but unable too. I have a
I have been pulling my hair out trying to make this work. I have
I have had been on a roller coaster trying to get ImageMagick to work

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.