Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Mitosis on November 19, 2003, 07:01 AM

Title: Cin??
Post by: Mitosis on November 19, 2003, 07:01 AM
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?
Title: Re:Cin??
Post by: iago on November 19, 2003, 08:50 AM
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.
Title: Re:Cin??
Post by: CupHead on November 19, 2003, 11:56 AM
Technically you can overload it to use any data type, but that's another topic.
Title: Re:Cin??
Post by: 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);
Title: Re:Cin??
Post by: Mitosis on November 19, 2003, 03:37 PM
So what happens if you press a?
Title: Re:Cin??
Post by: MoNksBaNe_Agahnim on November 19, 2003, 06:48 PM
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
----------------------------
Title: Monk: bad main
Post by: 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 **).
Title: Re:Monk: bad main
Post by: iago on November 20, 2003, 12:56 AM
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?
Title: Re:Cin??
Post by: Adron on November 20, 2003, 05:25 AM
I suggest developing a language named VC++ which is like C++ except it supports Void main - "Void main C++"!
Title: Re:Cin??
Post by: Banana fanna fo fanna on November 20, 2003, 03:04 PM
Quote from: Mitosis on November 19, 2003, 03:37 PM
So what happens if you press a?

Try it yourself.
Title: Re:Cin??
Post by: Mitosis on November 20, 2003, 03:36 PM
Oh boy press any key to continue and exit.
Title: Re:Cin??
Post by: Banana fanna fo fanna on November 20, 2003, 06:06 PM
That's exactly what should happen. Maybe you should check the contents of the variable now?
Title: Re:Cin??
Post by: Mitosis on November 21, 2003, 06:22 AM
int a;
cin >> a;
cout << "hello" << endl;

When I press anything Hello will show. How would I be able to make like questions?
Title: Re:Cin??
Post by: Banana fanna fo fanna on November 21, 2003, 07:07 PM
cout the question first, then cin the answers.
Title: Re:Cin??
Post by: toxic on November 21, 2003, 11:47 PM
getline(cin,apstring) > cin >> *.*
Title: Re:Cin??
Post by: Mitosis on November 22, 2003, 06:27 AM
Alright thanks guys, my friend also explained it to me too.
Title: Re:Cin??
Post by: CrAzY on November 22, 2003, 11:51 PM
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.
Title: Re:Cin??
Post by: 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
Title: Re:Cin??
Post by: Eibro on November 23, 2003, 09:58 AM
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.
Title: Eibro: what compiler are you living under?
Post by: Kp on November 23, 2003, 11:40 AM
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
       ret


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.
Title: Re:Cin??
Post by: Eibro on November 23, 2003, 11:52 AM
*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.
Title: Re:Cin??
Post by: Etheran on December 01, 2003, 04:37 PM
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;
}
Title: Re:Cin??
Post by: Adron on December 02, 2003, 04:24 PM
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 >>.