Simple code :: Cant figure out the problem...

This is a discussion on Simple code :: Cant figure out the problem... within the C++ Programming forums, part of the General Programming Boards category; Someone plz tell me what's wrong with my code... Code: #include <iostream> #include <stdlib.h> #include <stdio.h> #include <ctype.h> using namespace ...

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Exclamation Simple code :: Cant figure out the problem...

    Someone plz tell me what's wrong with my code...
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    using namespace std;
    
    
    int main (int argc, char *argv[])
    {
      cout << "Hi";  
      char input;
      cout << "Enter a character only" << endl;
      cin >> char;
      if isdigit(input)
      {
                       cout << "no no no...";
                      
      }  
      
      
      system ("PAUSE");
        return 0 ;
    }

    It gave lots of errors in Bloodshed Dev C++ 4.9.9.2.

    Thanx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cin >> char;
    Perhaps you meant:
    Code:
    cin>> input;
    >if isdigit(input)
    Parentheses?
    Code:
    if ( isdigit ( input ) )
    But a digit is also a character, so your program still has a logic error. Maybe you should ask for a non-digit, or a letter, since that is what most people want when they do not want a digit.
    My best code is written with the delete key.

  3. #3
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Oh sorry, ya i meant cin >> input;

    But the following code worked just fine using Turbo C++...

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<ctype.h>
    void main()
    {
    clrscr();
    char c;
    cout<<"Enter any character ...correction : non digit..."<<endl;
    lbl:
    cin>>c;
    if isdigit(c)
    {
    cout<<"Please enter non digit only"<<endl;
    goto lbl;
    }
    getch();
    }

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by pritin
    But the following code worked just fine using Turbo C++...
    Funny compiler.
    K.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    A start would be to change these...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    ...to these...

    Code:
    #include <cstdlib>
    #include <cstdio>
    #include <ctype.h> //Never heard of this one, so if it works this way, use it.
    And I noticed the "using namespace std;" statement isn't included.

    EDIT: edited out the "iostream's not there"; I'm obviously blind. :P
    It's Link, not linkofazeroth. The latter's just my username. The former's my online identity. Thank you.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> #include <ctype.h> //Never heard of this one, so if it works this way, use it.
    <ctype.h> becomes <cctype>, although either version is technically valid for all three of the headers you mentioned. They should not cause compile errors.

  7. #7
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Question

    The errors reurned for the following program are---

    Code:
    #include<iostream>
    #include<conio>
    #include<ctype>
    void main()
    {
    clrscr();
    char c;
    cout<<"Enter any character ...correction : non digit..."<<endl;
    lbl:
    cin>>c;
    if isdigit(c)
    {
    cout<<"Please enter non digit only"<<endl;
    goto lbl;
    }
    getch();
    }
    1. conio no such file or dir
    2.ctype no such file or dir
    3. 'main' must return 'int'
    4. In function 'int main(...)'; <-- no line number.
    5. cout undeclared
    6. cout undeclared
    7.clrscr undefined
    8. expected '(' before isdigit....

    ???Still need help...

    Plz help!!

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    hello, use the full file name 'filename.h', and be sure to open and close the brakets on the conditionals; that works:
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<ctype.h>
    void main()
    {
    //clrscr();
    char c;
    cout<<"Enter any character ...correction : non digit..."<<endl;
    lbl:
    cin>>c;
    if (isdigit(c))
        {
        cout<<"Please enter non digit only"<<endl;
        goto lbl;
        }
    getch();
    return 0;
    }
    Niara

  9. #9
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    using namespace std;
    
    
    int main()
    {
      cout << "Hi";  
      char input;
      cout << "Enter a character only" << endl;
      cin >> input;
      if (isdigit(input)!=0)
      {
       cout << "no no no...";
                      
      }  
      
      
        system ("PAUSE");
        return 0 ;
    }
    This worked for me in Dev++.

    Things to note: some files need to have the .h extension, however iostream and fstream are two exceptions. They work as long as you write 'using namespace std' at the top. Second, you might want to use cin.get(); to pause the program instead of system pause, since system is not considered a generic command for all OS.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    <conio.h> is not a standard C or C++ library header. Therefore, you leave it like it is: <conio.h>

    <ctype.h> is a standard C library header so in C++ you remove the .h and add the c in front of it: <cctype>

    In standard C++, everything in the standard libraries is placed in the std namespace. So when you include the standard io stream library header <iostream>, you need to qualify cout, cin, endl, etc. with std:: or add using namespace std; to the top of your program under the #includes.

    The old, non-standard headers that work on old compilers, like <iostream.h> do not require the std:: or using namespace std. However, modern compilers do not work with the outdated headers like that. You should not use your later code or Niara's code because it is old and outdated and will not compile on modern compilers. If your compiler is too old to compiler correct code, then getting a new compiler would be a better idea than writing non-standard code. The original code in the first post was just fine if you make the changes suggested by Prelude.

  11. #11
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Hey Thanks ppl!
    The code by trrenef is just fine, and just one last thing...

    How to check if an alphanumeral has bween entered...

    eg. when a4 is entered in the prev. prog., it does not say no no no...

  12. #12
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    What you are trying to do validate user input. If you are just beginning programming, this topic is best avoided until you become competent in what you are doing.

    It best just to assume the user actually inputs an integer or a character as specified in your program. Catching, or validating user input can be quite tricky.

    However, if this really bothers you look into using 'cin.ignore' to recieve only integer input. Also using 'cin.getline' to accept whitespace input. Also using 'atoi' to convert a string to an integer.

    One other thing, when you enter 'a4' at the command line, your program is expecting to receive ONE character only. 'a4' would be considered two characters, thus you have to account for that in your program.

    Perhaps you might want to look into 'strings' in the FAQ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  2. DirectMusic engine problem
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-17-2005, 05:12 PM
  3. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

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