Valhalla Legends Forums Archive | C/C++ Programming | Cin??

AuthorMessageTime
Mitosis
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?
November 19, 2003, 1:01 PM
iago
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.
November 19, 2003, 2:50 PM
CupHead
Technically you can overload it to use any data type, but that's another topic.
November 19, 2003, 5:56 PM
wut
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);
November 19, 2003, 7:45 PM
Mitosis
So what happens if you press a?
November 19, 2003, 9:37 PM
MoNksBaNe_Agahnim
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
----------------------------
November 20, 2003, 12:48 AM
Kp
[quote author=MoNksBaNe_Agahnim link=board=30;threadid=3703;start=0#msg30222 date=1069289325]
void main() [main function that doesn't need to return anything][/quote]As was bashed to death in another thread, this prototype is wrong. The correct and portable signature for main is int main (int, char **).
November 20, 2003, 3:40 AM
iago
[quote author=Kp link=board=30;threadid=3703;start=0#msg30272 date=1069299653]
[quote author=MoNksBaNe_Agahnim link=board=30;threadid=3703;start=0#msg30222 date=1069289325]
void main() [main function that doesn't need to return anything][/quote]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]

Or
int main( void ); is acceptable, isn't it?
November 20, 2003, 6:56 AM
Adron
I suggest developing a language named VC++ which is like C++ except it supports Void main - "Void main C++"!
November 20, 2003, 11:25 AM
St0rm.iD
[quote author=Mitosis link=board=30;threadid=3703;start=0#msg30141 date=1069277862]
So what happens if you press a?
[/quote]

Try it yourself.
November 20, 2003, 9:04 PM
Mitosis
Oh boy press any key to continue and exit.
November 20, 2003, 9:36 PM
St0rm.iD
That's exactly what should happen. Maybe you should check the contents of the variable now?
November 21, 2003, 12:06 AM
Mitosis
int a;
cin >> a;
cout << "hello" << endl;

When I press anything Hello will show. How would I be able to make like questions?
November 21, 2003, 12:22 PM
St0rm.iD
cout the question first, then cin the answers.
November 22, 2003, 1:07 AM
toxic
getline(cin,apstring) > cin >> *.*
November 22, 2003, 5:47 AM
Mitosis
Alright thanks guys, my friend also explained it to me too.
November 22, 2003, 12:27 PM
CrAzY
If you still need help... This explains it a bit more...

[code]

#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;

}
[/code]

Does that help? If any thing is wrong I Appoligize. Haven't worked with C in a while.
November 23, 2003, 5:51 AM
wut
[quote author=CrAzY link=board=30;threadid=3703;start=15#msg30848 date=1069566694]
[code]

#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;

}
[/code]
[/quote]

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
November 23, 2003, 9:04 AM
Eibro
[quote author=wut link=board=30;threadid=3703;start=15#msg30868 date=1069578293]
[quote author=CrAzY link=board=30;threadid=3703;start=15#msg30848 date=1069566694]
[code]

#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;

}
[/code]
[/quote]

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]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.
November 23, 2003, 3:58 PM
Kp
[quote author=Eibro link=board=30;threadid=3703;start=15#msg30901 date=1069603134]
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]

Your compiler must be more forgiving of bad practice than mine:[code]#include <stdio.h>
int main (int argc, char **argv) {
printf ("hmm\n");
}[/code]
[quote]int.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[/quote]
Result:
[code].globl _main
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $LC0, (%esp)
call _printf
leave
ret[/code]

Note 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.
November 23, 2003, 5:40 PM
Eibro
*shrug* I guess the compiler you're using doesn't conform to the standard:
[quote]In C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. [/quote]
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.
November 23, 2003, 5:52 PM
Etheran
[quote author=wut link=board=30;threadid=3703;start=0#msg30118 date=1069271131]
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);
[/quote] operator>> will read into a string until a space is encountered and buffer the rest of the string until it is called again. Example:
[code]
#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;
}
[/code]
December 1, 2003, 10:37 PM
Adron
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 >>.
December 2, 2003, 10:24 PM

Search