Thread: N00b to C++ here, have some general questions.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    24

    N00b to C++ here, have some general questions.

    So I've been taking a C++ in school and sometimes it seems like I've to turn to sources other than my prof for help. Hence after some googling, I'm here. Now I have a few questions.

    I've been reading my text and prof's lecture notes, and there are somethings that I can't get my mind around.

    1)
    Code:
    void printClassInfo( void )
    What exactly does the void in brackets do? Would you need the whole program to answer the question? I know what something like " void displayMessage( string courseName ) " does [sorry for not posting it in code, I figured it would "break the layout"].

    2) How exactly does the getline command work? An example to understand it's working would be very helpful, thanks in advance.

    3) What is the purpose of something like "using namespace std;" or "using std: :cout;" or "using std: :cin" etc?

    Any help on these three questions would be really helpful, and hopefully play an important role when the class advances further. Also, I have no previous exposure to C - I know it's a handicap, but I hope that it's not something that'll cripple me. Thanks in advance, guys/gals!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1. The void inside the ()'s shows what the function accepts. It accepts void, which means nothing. What is the difference between leaving out the void and not? Ah, this is going to be good confusion for you.

      For C, if you use empty ()'s in a function prototype, it means the actual parameter list to that function is undefined. It means you can call it with anything you want and the function just goes. The function itself can be defined to accept anything. This can lead to issues at runtime, so this is generally not how variable length args are done anymore.

      In C++ and the latest dialect of C, which is C99, as far as I know, there is no technical difference between (void) and (), but I might be totally wrong on that. C99 might still make the difference... actually I think it's militant on the difference.

      For when it doesn't matter (in C++), it's good to include the void since it makes your intentions explicit, but it's not necessary, and can be left out. Just make sure you remember exactly what language and dialect of said language you're using, and what you really intend.
    2. With regards to what? You said you're quite new, so I don't think you want an extremely detailed level of explanation, but I shall attempt to throw a bunch of information at you anyway and you can take whatever you are able to understand.

      On most systems, and in C (and subsequently C++), there are 3 files opened by default:

      • stdin
      • stdout
      • stderr


      All three files are usually refer in some ways to the console. Console input and output are handled by means of these "files". When you print information with cout, it's actually writing that data to the file stdout, which stands for Standard Out. Similary, stdin refers to Standard In. getline() simply reads a bunch of chars from this "file", possibly one by one.

      This can get quite complicated, so for now, just accept it works, and get very used to it.
    3. To avoid naming conflicts, the concept of namespaces was incorporated into C++. This means you can take classes, functions, and variables and put them in a namespace. There's a namespace named std. That namespace includes a bunch of basic things you use all the time, like cout and cin. To use them you would do something like this:

      Code:
      std::cout << "hello world" << std::endl;
      Since typing std:: can be tiresome, you can tell the compiler to automatically check out certain namespaces or certain elements inside certain namespaces when it comes across an unknown element.

      Code:
      using namespace std;
      
      ...
      
      cout << "hello world" << endl;
      When cout and endl are reached by the compiler, it knows to check namespace std to see if cout and endl happen to be there. If it finds them there, it uses those particular elements.

      Again, namespaces are there so you can avoid naming conflicts. You might not make a class with the name cout, given that you know one exists, but you might accidentally make one for something less well known and that might cause issues. Namespaces allows you to organize your code.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bash View Post
    So I've been taking a C++ in school and sometimes it seems like I've to turn to sources other than my prof for help. Hence after some googling, I'm here. Now I have a few questions.

    I've been reading my text and prof's lecture notes, and there are somethings that I can't get my mind around.

    1)
    Code:
    void printClassInfo( void )
    What exactly does the void in brackets do? Would you need the whole program to answer the question? I know what something like " void displayMessage( string courseName ) " does [sorry for not posting it in code, I figured it would "break the layout"].
    In C++, the void in brackets is not NECESSARY, but it is a way to indicate explicitly that "this function takes no parameters". In C there is a difererence between a function declared as
    Code:
    void printClassInfo()
    and
    Code:
    void printClassInfo(void)
    - the first one has "unspecified parameters" (could be zero, one or ten - no way for the compiler to tell what is right and wrong of these). The second one has no parameters - that's it, the compiler will complain if you give it any parameter(s).

    2) How exactly does the getline command work? An example to understand it's working would be very helpful, thanks in advance.
    Technically, it's not a "command", but a function.

    Essentially, it reads data into a C-string (char array) from the input stream (usually cin) until it reaches a newline (or some other delimiter specified by the third parameter) - or it reaches the size specified.

    There is also a getline that operates on a C++ std::string type - this is not part of the iostream member functions, but it essentially does the same thing - you pass a stream as one of the parameters instead.

    3) What is the purpose of something like "using namespace std;" or "using std: :cout;" or "using std: :cin" etc?
    When working on very large projects, you sometimes get what's called "name clashes", where two programmers decide to call a function the same thing. By using different namespaces, these name-clashes can be limited to a smaller portion of code. A namespace may be a functional unit. E.g. the graphics part of an OS may have it's own namespace, the kernel has another namespace.

    In a large application that does order, stock and invoicing, the ordering system has one namespace, the stock counting another, and the invoicing part a third namespace. That means that the variabl "current_customer" in the ordering system is a distinct different variable than "current_customer" in the invoice system, for example - because one is, when you write it's full name is "order::current_customer" and the other "invoice::current_customer".

    You may find that there is a "sum_customer_order" in both the ordering system and in the invoice system - but they do different things: in the ordering system, it calculates the total of the order as the customer orders. In the invoice stage, you only invoice for the items actually delivered, and any back-order items will not be included in this stage - they are invoiced when they are delivered.


    Any help on these three questions would be really helpful, and hopefully play an important role when the class advances further. Also, I have no previous exposure to C - I know it's a handicap, but I hope that it's not something that'll cripple me. Thanks in advance, guys/gals!
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    Thanks for both the replies, MacGyver and matsp I now have an understanding of it and pray they don't go away.

    About the getline function - can you use it to store a string in an array? For example, let's say I input 04953045, can I use the getline function to store it in an array of length l-1 (here's it's 8, duh! :P) so that I can actually have different numbers in a[0],a[1],etc? If so, how would I do it?

    [Oh, btw, everytime I hear that something's a function, I end up thinking f(x) in math and my mind can't grasp the meaning quite well ={]

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bash View Post
    About the getline function - can you use it to store a string in an array? For example, let's say I input 04953045, can I use the getline function to store it in an array of length l-1 (here's it's 8, duh! :P) so that I can actually have different numbers in a[0],a[1],etc? If so, how would I do it?
    istream::getline stores the input as characters in a char array, so cin.getline(a, 10) would give you an array of a[0] = '0', a[1] = '4', ... a[7] = '5', a[8] = '\0'.

    [Oh, btw, everytime I hear that something's a function, I end up thinking f(x) in math and my mind can't grasp the meaning quite well ={]
    Yes, mathematical and programming functions are generally not the same thing. In some languages, such as Pascal, there are "procedures" and "functions". A procedure is the same as a C function that returns nothing (void function), and a "function" in Pascal is the same as something that returns a value (but not necessarily a numerical value - it could be "anything").

    But it's best if you "program your brain" to think of functions in programming and that they aren't the same as functions in mathematics.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't see why the math model of functions should be confusing when applied to programming.

    Take something simple: f(x) = x + 5

    It accepts a parameter x, and returns (x + 5). You can program something similar quite easily:

    Code:
    int f(int x)
    {
    	return x + 5;
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, sure math functions are WITHIN the functions in programming - it's just that math functions are much more restricted than programming functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    So I've been taking a C++ in school and sometimes it seems like I've to turn to sources other than my prof for help.
    This is normal, especially if it's your first programming class. The concepts are new, the terminology is new, and (assuming this is a university class) you will be covering lots of ground very quickly.

    And, it will probably require more "homework time" than your typical class. It's not that you will be assigned lots of homework, but you will frequently "get stuck" on some small issue or bug, and that can eat-up many hours. It would be a good idea to get into a study group (if that's not considered "cheating").
    Last edited by DougDbug; 10-02-2007 at 02:58 PM.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    So I have this homework assignment that's due on Friday, and I had started working on it. Basically what we have to do is accept a number (like 95468) and write a program that outputs the sum of the digits, the biggest digit, the smallest digit, the first digit and the last digit. So far I've figured that if I use modulus and keep dividing by ten, I can separate teh values. But for that, the only way I can write it is to have a set number of digits (like it has to be a 5-digit number). Overall it doesn't seem to work right - I shall post the code and any way to optimize it or any suggestions would be helpful.

    There is also a second part to the question - It's the "Press Y to continue or N to quit" The program would also quit if you entered -99999. To do this, would I envelop the whole body of it in an if-else loop? Where would I place it?

    Code:

    Code:
    #include <iostream> 
    #include <string>
    using namespace std;
    
    int main ()
    {
    	int iX;
    	int iSmall;
    	int iBig;
    	int iY;
    	int iSmallestDigit = 9;
    	int iTestNum;
    	int iDigit;
    	int iLargestDigit = 0;
    	int iSum = 0;
    	int iSumDump;
    
    	cout << "Class Information --"
    		 << "\n CIS 25 - ++ Programming"
    		 << "\n Laney College" << endl;
    
    	cout << "\nAssigment Information --"
    		 << "\n Assigment Number: Lab 03 Exercise #1"
    		 << "\n Written by: Amol Mundayoor"
    		 << "\n Due Date: 09/26/07" << endl;
    
    	cout << "\n\nEnter a 5-digit Integer: ";
    	cin  >> iX;
    
    
    		if (iX % 2 == 0)
    			{
    			cout << iX << " Is an even ";
    			}
    		else
    			{
    			cout << iX << " Is an odd ";
    			}
    		if (iX > 0)
    			{
    			cout << "and positive number\n";
    			}
    		else if (iX < 0)
    			{
    			cout << "and negative number\n";
    			}
    		else
    			{
    			cout << "Invalid Input" << endl;
    			}
    
    		iSmall = iX % 10;
    			cout << "The least significant digit is " << iSmall <<endl;
    
    		iBig = iX/10000;
    			cout << "The most significant digit is " << iBig << endl;
    
    		iTestNum = iX;
    			
    		do
    		{
    		
    		iDigit = iTestNum % 10;
    		
    		if ( iSmallestDigit > iDigit )
    			
    			{
    				iSmallestDigit = iDigit;
    			}
    		
    		iTestNum /= 10;
    		
    		} while ( iTestNum );
    	
    			cout << "Smallest Digit: " << iSmallestDigit << endl;
    		
    		iY = iX;
    
    		do
    		{
    		
    		iDigit = iY % 10;
    		
    		if ( iLargestDigit < iDigit )
    			
    			{
    				iLargestDigit = iDigit;
    			}
    		
    		iY /= 10;
    		
    		} while ( iY );
    	
    			cout << "Largest Digit: " << iLargestDigit << endl;
    
    		
    		iSumDump = iX;	
    		for (iSumDump = iX % 10; iX /= 10; iSum=iSum+iSumDump);
    			cout << "The sum of Digits is: "<< iSum << endl;
    
    
      return 0; 
    }
    And my for loop doesn't seem to work either - where is it not logically right? =/ Thanks a bunch, at least I'm getting started here ^^;

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are right, the for-loop doesn't work right. You are not updating the "current digit" in iSumDump, and you are not counting the final (most significant) digit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    I was hoping to use iSumDump as a temp value. I wanted to keep storing the remainders of teh division in iSumDump and keep adding to iSum. For example,

    If the number is 94602, I want iSumDump to have the value "2" in the first run, "0" in teh second, "6" in the third, etc. And at the end of the loop, before it gets out, I'd keep adding it to iSum (iSum would be updated with "2" and then the rest would be added on. Would such an operation work with a for loop or should I do something else, like a do...while?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and in your current implementation, you are taking the first digit into iSumDump, then adding that until your number divided by 10 is zero. So if you enter 12345, you add the number five four times to iSum.

    There's nothing wrong with the variable as such, and if the above was what you WANT to do, then it would be a fine solution. However, you probably want to add 1 + 2 + 3 + 4 + 5 (or, rather, to make life easy, 5 + 4 + 3 + 2+ 1), not 5 + 5 + 5 + 5.

    So you need to do two things:
    1. Make sure that iSumUpdate is representing the current digit in the number, not always the least significant.
    2. Make sure ALL digits are accumulated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    OHHHH, I see what I was doing wrong. After fiddling with the for loop, I couldn't come up with something that actually worked. It's kind of a bummer to me that my for loop didn't work, but I made a do while (it felt a LOT more simple to make one)

    Code:
    do
    {
    iSumDump = iX &#37; 10;
    iSum = iSumDump+iSum;
    iX /= 10;
    } while (iX);
    I don't want to ask anyone to do anything for me, but I have no idea on how the right for loop would look like. Can anyone show the actual working loop? I know how to do it in do...while, but I'm totally lost when I do it in the for loop. Thanks a lot for all your help
    Last edited by Bash; 10-03-2007 at 02:25 PM.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The for loop would depend upon what you set iX to. Just that block you posted would look like something like this:

    Code:
    for(;iX;iX /= 10)
    {
    	iSumDump = iX &#37; 10;
    	iSum = iSumDump+iSum;
    }
    The main difference with for and while vs do-while is that a for/while loop will have its condition checked prior to entering the loop. On the other hand, a do-while will execute once regardless if the condition is true or not. This inherently means a do-while loop will always execute at least once.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    That's interesting, because I've never come across a for-while loop before.

    What if I wanted my program to accept an n-digit integer instead of a 5-digit integer (I didn't know how to solve it unless I assumed that the entered integer is a 5-digit one)? Would I still use
    "istream::getline"? Or is there a way to convert the number to a string and then work with it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password program / general programming questions
    By plr112387 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2007, 10:10 PM
  2. n00b questions
    By C+noob in forum C++ Programming
    Replies: 43
    Last Post: 07-09-2005, 03:38 PM
  3. A Few General C Questions
    By Jedijacob in forum C Programming
    Replies: 13
    Last Post: 02-17-2005, 02:47 AM
  4. Taking up OpenGL(extreme n00b questions)
    By Xterria in forum Game Programming
    Replies: 3
    Last Post: 03-26-2004, 04:49 PM
  5. FAQ: Examples of good questions (General)
    By Prelude in forum FAQ Board
    Replies: 1
    Last Post: 10-11-2002, 08:57 PM