Jump to content

Basic Input and Output C++

- - - - -

  • Please log in to reply
6 replies to this topic

#1
539

539

    Newbie

  • Members
  • Pip
  • 5 posts
If you compile this code you will get to text "Hello World!" followed by a line asking you to press any key to continue:


#include <iostream>

int main()

{

cout << "Hello World!\n\n";

system("pause");

}


Now you can add a parameter and ask the user to enter a number, then display that number:


#include <iostream>

int main(int x) // The parameter(s) for the main function are x

{

cout << "Enter a number: ";

cin >> x; // Stores the input by the user to x

system("cls"); // Clears the CMD Promt window

cout << "Your number: " << x << "\n\n"; // Displays x

system("pause");

}


If you are asking the user for a hex value you would do this (This is the same as above just allows the user to enter a hex value)


#include <iostream>

using namespace std;

int main(int x) // The parameter(s) for the main function are x

{

cout << "Enter a number: ";

std::cin >> hex >> x; // Stores the input by the user to x

system("cls"); // Clears the CMD Promt window

cout << "Your number: "<< hex << x << "\n\n"; // Displays x

system("pause");

}


If you don't use the namespace std and use the second code in this tutorial and the user enters a hex value the program will close.

END TUTORIAL

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
system("cls") does not work on non-Windows systems. Neither does system("pause").
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
539

539

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

system("cls") does not work on non-Windows systems. Neither does system("pause").

I wouldn't know because the only thing I've had is XP and 7

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
He seems to be spamming that all over the net -- see this one
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
539

539

    Newbie

  • Members
  • Pip
  • 5 posts

Ancient Dragon said:

He seems to be spamming that all over the net -- see this one

LMFAO! Two sites, that's not spamming

#6
grisha

grisha

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Anyway. The code you've posted is wrong.

#7
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
That code is wrong. Perhaps we should compile and test before posting? Or stick to reading some material instead of trying to generate it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users