Thread: Beginner Error

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs down Beginner Error

    Code:
    #include <iostream>
    #include <string>
    
    void main ()
    {
    	char studentId[7] = "";
    	short numOfChars = 0;
    
    	cout << "Enter Student ID: ";
    	cin.getline(studentId, 7);
    
    	numOfChars = strlen(studentId);
    
    	if(numOfChars == 6)
    		if(stricmp(studentId, "ABC123") == 0)
    			cout << "\nAccepted";
    		else
    			cout << "\n Not Accepted" << endl;
    	else
    		cout << "The Student ID must be atleast 6 chars" << endl;
    	//endif
    
    }
    Hi,

    I am getting 4 errors with this and I have spent long time but cannot see my errors.

    It gives four sayin that the cin and cout are undeclared identifiers?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    cin and cout are in the std:: namespace.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    You have not included the standard library

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void main ()
    {
    	char studentId[7] = "";
    	short numOfChars = 0;
    
    	cout << "Enter Student ID: ";
    	cin.getline(studentId, 7);
    
    	numOfChars = strlen(studentId);
    
    	if(numOfChars == 6)
    		if(stricmp(studentId, "ABC123") == 0)
    			cout << "\nAccepted";
    		else
    			cout << "\n Not Accepted" << endl;
    	else
    		cout << "The Student ID must be atleast 6 chars" << endl;
    	//endif
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by te5la
    You have not included the standard library
    That is not accurate since the <iostream> standard header was included, though your fix is one correct solution. robwhit's analysis is accurate, so an alternative fix is to fully qualify the names as std::cin and std::cout.

    Also, note that void main() should be int main(), and that stricmp() is not in the standard library. strcmp(), on the other hand, can be found in <cstring> (not <string>).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    There is another mistake that was missed. Your nested elses are wrong. Should follow this pattern...
    if
    else if
    else

    you can have multiple else if, but only one else.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Darryl
    There is another mistake that was missed. Your nested elses are wrong. Should follow this pattern...
    No, that is not a mistake. For one thing, it is a matter of good style rather than something that must be done, but more importantly, the structure of the code is such that that pattern is not applicable. There is simply no if statement nested in an else block.

    EDIT:
    Quote Originally Posted by Darryl
    you can have multiple else if, but only one else.
    Look again: the code is well indented, though adding some braces probably would help avoid such a mistake in reading the code.
    Last edited by laserlight; 09-19-2008 at 02:38 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Darryl View Post
    There is another mistake that was missed. Your nested elses are wrong. Should follow this pattern...
    if
    else if
    else

    you can have multiple else if, but only one else.
    The nesting is correct.
    Code:
    if
      if
      else
    else
    Braces make this easier to see, though.
    Code:
    if {
      if {
      } else {
      }
    } else {
    }

    What hasn't been commented on yet is the void main mistake. main returns an int, always.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CornedBee
    What hasn't been commented on yet is the void main mistake. main returns an int, always.
    Tsk tsk, read post #4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    Tsk tsk, read post #4.
    And post #2.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    OK, I'm blind this evening.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are a beginner you should be learning C++ strings, not C style character arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM