• Welcome to Valhalla Legends Archive.
 

Cin??

Started by Mitosis, November 19, 2003, 07:01 AM

Previous topic - Next topic

Mitosis

Alright thanks guys, my friend also explained it to me too.

CrAzY

#16
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.
CrAzY

wut

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

Eibro

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.
Eibro of Yeti Lovers.

Kp

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Eibro

#20
*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.
Eibro of Yeti Lovers.

Etheran

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

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 >>.