Comparing Value of Int to String

This is a discussion on Comparing Value of Int to String within the C Programming forums, part of the General Programming Boards category; Say for example you have a menu set up, and you have a limited amount of choices. And you only ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    38

    Comparing Value of Int to String

    Say for example you have a menu set up, and you have a limited amount of choices. And you only want to allow ..say 1-4 and cancel out all other options. Like you have an error message come up if they hit anything other than 1-4. Anyway to do that. I went through the forum search and found a few things sorta like I'm asking, but I'm new to c++ and could exactly relate it enough for use.

    Like I enter the highed ascii character in int value

    dec value 5 to 122 in ascii chart being alittle z.
    anyway to directly error out the keys themselves?

    Or how else would you go about it?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    can someone please put this in c++ board, I by mistake put it in the c board. pretty please. thanks.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Aw, you're no fun. I was going to roll out the clever scanf scanset solution:
    Code:
    #include <stdio.h>
    
    void discard(FILE *in);
    
    int main(void)
    {
      char option;
    
      printf("1) One\n2) Two\n3) Three\n4) Four\n> ");
      fflush(stdout);
      while (scanf("%[1234]", &option) != 1) {
        fprintf(stderr, "Invalid option, try again\n> ");
        clearerr(stdin);
        discard(stdin);
      }
    
      return 0;
    }
    
    void discard(FILE *in)
    {
      int ch;
    
      while ((ch = fgetc(in)) != EOF && ch != '\n')
        ;
    }


    But here's a potential C++ solution as well, since this thread will probably be moved:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int option;
    
      cout<<"1) One\n2) Two\n3) Three\n4) Four\n> ";
      while (!(cin>> option) || option < 1 || option > 4) {
        cerr<<"Invalid option, try again\n> ";
        cin.clear();
        cin.ignore(cin.rdbuf()->in_avail(), '\n');
      }
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    hey thanks for the quick reply heh even before it was moved heh.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    #include <stdio.h>
    or you could just change this to

    Code:
    #include <csdtio>
    using namespace std;
    And have the C solution work in C++.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And have the C solution work in C++.
    And then get run off of the C++ forum because you used scanf. Purists are annoying, aren't they?

    By the way, it's <cstdio>. Case is important, but spelling is too.
    My best code is written with the delete key.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Why yes... purists are annoying...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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