C++ Program to output type of input entered (int, char, etc...)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jurmav
    New Member
    • Sep 2007
    • 5

    C++ Program to output type of input entered (int, char, etc...)

    i am beginner in using turbo c++. and i want to finish my coding. but then i have no idea on how can i make it.

    my program is about if u enter a number it will print "u enter numeric", then if u enter a word the output would be "u enter a string" then last if u enter both numeric and letter the output would be " u enter a variable".

    hope i could get help to someone who really have an idea about the program.
    my warmest thanks to someone who could give me some part of the codings.......
    Last edited by sicarie; Sep 25 '07, 01:36 PM. Reason: bold and italics are annoying to me
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by jurmav
    HELP!!!!! i am beginner in using turbo c++. and i want to finish my coding. but then i have no idea on how can i make it.

    [B]my program is about if u enter a number it will print "u enter numeric", then if u enter a word the output would be "u enter a string" then last if u enter both numeric and letter the output would be " u enter a variable".

    hope i could get help to someone who really have an idea about the program.
    my warmest thanks to someone who could give me some part of the codings.......
    All you need to do is to accept input as string,and then if whole string contains only numbers then it's a numeric,if it only contain letters then it is a word,otherwise if it contains letter and numeric it's a variable.

    If you get stuck with coding please post what have you tried and we will try to help you.

    Savage

    Savage

    Comment

    • jurmav
      New Member
      • Sep 2007
      • 5

      #3
      Originally posted by Savage
      All you need to do is to accept input as string,and then if whole string contains only numbers then it's a numeric,if it only contain letters then it is a word,otherwise if it contains letter and numeric it's a variable.

      If you get stuck with coding please post what have you tried and we will try to help you.

      Savage

      Savage
      the i have here is:

      #include<stdio. h>
      #include<string .h>
      #define p printf
      main()
      {char a;

      clrscr();
      p("input a word: ");
      scanf("%s",a);
      printf("\n you entered a character");
      getch();
      }

      i don't know what will be the next step.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by jurmav
        the i have here is:

        #include<stdio. h>
        #include<string .h>
        #define p printf
        main()
        {char a;

        clrscr();
        p("input a word: ");
        scanf("%s",a);
        printf("\n you entered a character");
        getch();
        }

        i don't know what will be the next step.
        This is no good.

        instead of a single char you need a array of chars.

        There are two types of arrays in c:stack arrays and dynamic arrays.

        Size of stack array cannot be changed during execution,which is not true for dynamic arrays.To build a dynamic array(you don't know size of the string that user will enter) you just declare a pointer to a char:

        char *a.*b;/*You will need 2 arrays

        If you want to capture a whole sentence not just a part of the string(scanf breakes on a white space) you use gets() to read in a string.After that you can use strlen to determine the size of first array and the allocate second one:

        b=(char*)malloc (sizeof(char)*( strlen(a)+1))

        Now you can use as a simple stack based arrays.When you have done this post again.

        PS:It's recomended that you read some tutorials about arrays.

        Savage

        Comment

        • jurmav
          New Member
          • Sep 2007
          • 5

          #5
          Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
          this time i am doing a new program.This is all about the entering of a fname,lname,mid name and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
          the output would be like this.
          method 1: fname lname midname sub/sect

          method II: fname|lname|mid name|sub/sect

          method III: number of character entered |fname|lname|mi dname|sub/sect

          method IV: fname|lname|mid name|sub/sect number of character entered

          method V:fname|lanme|m idname|sub/sect #

          my problem is from method 3 and 4.
          i have finished on my codings but then.
          the fname,lname,mid name and the sub/section are not being read by my strlen commands.
          this is what my codings are:

          printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+" "+whois[x].midname+" "+ whois[x].ss));


          the error was : Invalid pointer addition in function method.
          hope i can get a response here.
          thanks in advance.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by jurmav
            Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
            this time i am doing a new program.This is all about the entering of a fname,lname,mid name and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
            the output would be like this.
            method 1: fname lname midname sub/sect

            method II: fname|lname|mid name|sub/sect

            method III: number of character entered |fname|lname|mi dname|sub/sect

            method IV: fname|lname|mid name|sub/sect number of character entered

            method V:fname|lanme|m idname|sub/sect #

            my problem is from method 3 and 4.
            i have finished on my codings but then.
            the fname,lname,mid name and the sub/section are not being read by my strlen commands.
            this is what my codings are:

            printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+" "+whois[x].midname+" "+ whois[x].ss));


            the error was : Invalid pointer addition in function method.
            hope i can get a response here.
            thanks in advance.
            Ok!
            This is C++ so you are permitted to add two char arrays.
            If you like then call strcat or try to do Operator Overloading.
            Good Luck :-)

            Kind regards,
            Dmjpro.

            Comment

            • jurmav
              New Member
              • Sep 2007
              • 5

              #7
              Re: C++ Program to output type of input entered (int, char, etc...)
              Gud day! i quited on the program that i was doing about looking for if the input was variable,string or numeric.
              this time i am doing a new program.This is all about the entering of a fname,lname,mid name and the subject& section of the student i used an array and.where in i have to use a method in looking out the output or the record.
              the output would be like this.
              method 1: fname lname midname sub/sect

              method II: fname|lname|mid name|sub/sect

              method III: number of character entered |fname|lname|mi dname|sub/sect

              method IV: fname|lname|mid name|sub/sect number of character entered

              method V:fname|lanme|m idname|sub/sect #

              my problem is from method 3 and 4.
              i have finished on my codings but then.
              the fname,lname,mid name and the sub/section are not being read by my strlen commands.
              this is what my codings are:

              printf(""%d", strlen(whois[x].fname+ " "+ whois[x].lname+whois[x].midname+ whois[x].ss));


              the error was saying: Invalid pointer addition in function method.
              hope i can get a response here.
              thanks in advance.

              Comment

              Working...