In the book I have c++ for dummies, Im at the part where its talking about the "cin", Im kind of confused like it says you can input stuff in there, but Im not sure how to. Like do you put variables inside of it or what?
cin reads input from the keyboard
int a;
cin >> a;
That'll wait for them to enter a number and press enter, then put that value in a. Most variable types can be used, like char[], double, etc.
Technically you can overload it to use any data type, but that's another topic.
the >> operator can only be used to read one line as cannot process whitespace characters; the global function getline() is also available to read input, e.g.
char var[100];
cin.getline(var, 99);
So what happens if you press a?
example...
void main() [main function that doesn't need to return anything]
{
int x; [your variable is an integer and is delcared as x as is used in alg]
cout << "Input the value of x: "; [this is outputting whats in the quotes]
cin >> x; [this is what accepts your input -- note the direction of ">>" opposite of cout's]
cout << endl << "value of x is now: " << x; [skips down a line and outputs your new value of x]
}
----------------------------
{output on screen}
Input the value of x: 2
value of x is now: 2
----------------------------
Quote from: MoNksBaNe_Agahnim on November 19, 2003, 06:48 PM
void main() [main function that doesn't need to return anything]
As was bashed to death in another thread, this prototype is wrong. The correct and portable signature for main is
int main (int, char **).
Quote from: Kp on November 19, 2003, 09:40 PM
Quote from: MoNksBaNe_Agahnim on November 19, 2003, 06:48 PM
void main() [main function that doesn't need to return anything]
As was bashed to death in another thread, this prototype is wrong. The correct and portable signature for main is int main (int, char **).
Or
int main( void ); is acceptable, isn't it?
I suggest developing a language named VC++ which is like C++ except it supports Void main - "Void main C++"!
Oh boy press any key to continue and exit.
That's exactly what should happen. Maybe you should check the contents of the variable now?
int a;
cin >> a;
cout << "hello" << endl;
When I press anything Hello will show. How would I be able to make like questions?
cout the question first, then cin the answers.
getline(cin,apstring) > cin >> *.*
Alright thanks guys, my friend also explained it to me too.
If you still need help... This explains it a bit more...
#include <iostream.h> //where 'cin' and 'cout' come from ;-P
int main()
{
int a;
cout<<"What is your favorite number?\n";
cin>>a;
cout<<"\nYour Favorite Number is: "<<a;
}
Does that help? If any thing is wrong I Appoligize. Haven't worked with C in a while.
Quote from: CrAzY on November 22, 2003, 11:51 PM
#include <iostream.h> //where 'cin' and 'cout' come from ;-P
int main()
{
int a;
cout<<"What is your favorite number?\n";
cin>>a;
cout<<"\nYour Favorite Number is: "<<a;
}
To be more specific, cin and cout are objects of istream and ostream, respectively. It is also conventional to return an exit code to the operating system, e.g. return EXIT_SUCCESS
Quote from: wut on November 23, 2003, 03:04 AM
Quote from: CrAzY on November 22, 2003, 11:51 PM
#include <iostream.h> //where 'cin' and 'cout' come from ;-P
int main()
{
int a;
cout<<"What is your favorite number?\n";
cin>>a;
cout<<"\nYour Favorite Number is: "<<a;
}
To be more specific, cin and cout are objects of istream and ostream, respectively. It is also conventional to return an exit code to the operating system, e.g. return EXIT_SUCCESS
It's perfectly acceptable to specify a return type of int and return nothing. IIRC, in this case a return value of 0 is implied.
Quote from: Eibro on November 23, 2003, 09:58 AM
It's perfectly acceptable to specify a return type of int and return nothing. IIRC, in this case a return value of 0 is implied.
Your compiler must be more forgiving of bad practice than mine:
#include <stdio.h>
int main (int argc, char **argv) {
printf ("hmm\n");
}Quoteint.c: In function `main':
int.c:2: warning: unused parameter `argc'
int.c:2: warning: unused parameter `argv'
int.c:4: warning: control reaches end of non-void function
Result:
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $LC0, (%esp)
call _printf
leave
retNote that register eax is not modified before returning to the system, so a return of zero was
not implied in this case. IMO, it's good practice to always return what you intend to return, not rely on the compiler to figure it out and hope that it returns what you want.
*shrug* I guess the compiler you're using doesn't conform to the standard:
QuoteIn C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution.
http://www.research.att.com/~bs/bs_faq2.html#void-main
I've never actually tried it with my compiler, seems as if it'd be better to explicitly return something anyway.
edit: Oh shoot, I meant it's acceptable for
main() to not explicitly return anything.
edit: Kp, were you compling that as C or C++? I'm pretty sure C doesn't allow this implicit return.
Quote from: wut on November 19, 2003, 01:45 PM
the >> operator can only be used to read one line as cannot process whitespace characters; the global function getline() is also available to read input, e.g.
char var[100];
cin.getline(var, 99);
operator>> will read into a string until a space is encountered and buffer the rest of the string until it is called again. Example:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
char theBuf[50] = { 0 };
char anotherBuf[50] = { 0 };
cin >> theBuf; //user inputs "hello friend" and theBuf is filled with "hello"
//the input buffer is filled with the rest of the string and waits
//for another input to be called
cin >> anotherBuf; //user doesn't get a chance to input here, the the next part of
//the string is put into anotherBuf until another space is
//encountered and so on and so forth..
cout << theBuf << '\n';
cout << anotherBuf << endl;
return 0;
}
Whether >> reads entire lines is unknown. What we do know is that when calling the predefined >> for strings/char arrays, it will read one word at a time. You're free to redefine it any way you like. You could define a "line" class which works exactly like a string except it reads one line at a time with istream::operator >>.