LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2004, 02:38 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Newbie trying to learn C++. How to use variables.


Ok, i've been learning how to use C++ from this site http://www.cprogramming.com/tutorial/lesson1.html
And I've gotten not far, because its too complicated on "variables". How exactly do u do variables??? This is what I've cungered up so far.

#include <iostream.h>
int
main()
{
int 3;
cout<<"6:";
cin>>5;
cout<<"randomthing: "<<7;
return 0;
}

This is the example it showed me...
#include <iostream.h>
int
main()
{
int thisisanumber;
cout<<"Please enter a number:";
cin>>thisisanumber;
cout<<"You entered: "<<thisisanumber;
return 0;
}

im guessing im supposed to fill in what it tells me, but it keeps giving out an error, whenever try to compile with g++, I just dont get "chars" and "vars"

Also, could someone redirect me to a site, of an easier tutorial, its just too complicated.
 
Old 07-21-2004, 03:40 AM   #2
lone_nut
Member
 
Registered: Dec 2003
Location: Denmark
Distribution: Mandrake
Posts: 179

Rep: Reputation: 30
you declare a variable called 3 and address it as six?
You cant declare are variable as a number.

new changed number:

[code]
#include <iostream.h>
int main()
{
int somenumber;
cout<<"6:";
cin>>somenumber;
cout<<"randomthing: "<<somenumber;
return 0;
}
 
Old 07-21-2004, 05:04 AM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
No, type in the listing given to you exactly as you read it!

DO NOT REPLACE THE STATEMENT "thisisanumber" WITH A NUMBER!

Variables a like buckets - you put something in them for storage.
You give each bucket a name, like you and I have names on this forum.
This name must be unique (as on this forum) so we can always identify our buckets without any doubt.
There are rules about the names you can give variables, just as I am not allowed to use offensive words in my name on this forum.
One of the rules is that you cannot start a variable's name with a number, which is what you did by typing the line:
int 3;

There are different types of bucket to hold different types of value.

The statement:
int thisisanumber;
creates a bucket called "thisisanumber" which will hold an integer, that is a whole number between -2147483648 and 2147483647. The type of bucket is called 'int'

Different types of bucket include char and float, so we can store letters and decimal values as well as integers.
(I would write
float thisisadecimal;
to create a bucket to hold a decimal called thisisadecimal)
The general format to declare a variable is like this:

type identifier;

The rest of the program:

cout<<"Please enter a number:";

This line will print "Please enter a number:" on your screen.
Anything contained with double quotes like this is a string literal.
It will appear exactly as typed.
THIS IS A VERY IMPORTANT DIFFERENCE!

cin>>thisisanumber;

The program will now wait for you to enter a number on the screen and press return. When you press return (and assuming you have typed a number) the computer will put this number into the bucket called thisisanumber.

cout<<"You entered: "<<thisisanumber;

The computer will now print on your screen "You entered:" followed by the contents of the bucket called thisisanumber.

Some other stuff you can do with variables.
Also note that anything appearing between // and a newline will be ignored.
This is referred to as a comment and allows me to annotate the code.
You can copy and paste the code below into a text file and then compile it.

Code:
#include <iostream.h>
int main()
{
  int thisisanumber;
  thisisanumber = 99;  // fill the bucket with the value 99

  int anothernumber;   // create another bucket, called anothernumber
  anothernumber = 1;  // fill another number with the value 1

  int yetanothernumber; // create a third bucket ...

  // fill yetanothernumber with the value contained in thisisanumber added to
  // the value contained in anothernumber

  yetanothernumber = thisisanumber + anothernumber;

  // show the value contained in yetanothernumber on the screen
  // note the difference between yetanothernumber appearing in 
  // quotes, which will show the word "yetanothernumber" and
  // yetanothernumber appearing without quotes which allows us to
  // access its contents
  cout << "The value contained in yetanothernumber is : " << yetanothernumber;

  return 0;
}
You might also want to know that you can't do this:
cin >> 5;
You need to have something that you can fill at the end of the statement.
You can fill a variable but you can't fill a number or a value, in the same way you can fill a bucket but you can't fill water.

Where you wrote;
cout<<"randomthing: "<<7;
however, you can output a value, just as you can output a variable.
On screen this will show 'randomthing: 7' on your screen.


Well, I hope all that helped a bit anyway.
If you can find it "The Complete Idiots Guide to c++" by Paul Snaith is a fairly good book to learn from.
Amazon reviews:
http://www.amazon.com/gp/product/cus...=ATVPDKIKX0DER

Last edited by dakensta; 07-21-2004 at 05:18 AM.
 
Old 07-21-2004, 10:08 PM   #4
LavaDevil94
LQ Guru
 
Registered: Jul 2003
Distribution: Gentoo 2004.2: Who needs exmmpkg when you have emerge?
Posts: 1,795

Rep: Reputation: 47
I strongly recommend using C for doing that stuff. It's simpler, and with what you're doing, it won't make a difference. Here's a program that takes input from the user and prints what he/she entered to the screen (written in C):
Code:
#include <stdio.h>

int number;
int main()
{
      printf("Please enter a number, only single-digit please (otherwise the program goes haywire\n");
      printf("Your number: ");
      scanf("%d", &number);
      printf("Thank you. Your number was %d.\n", number);
      printf("Bye!\n");
      return 0;
}

Last edited by LavaDevil94; 07-21-2004 at 10:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Learn by doing advice for newbie legacyprog Linux - Newbie 5 03-08-2004 07:25 AM
Newbie needs to learn the ropes!! Enoxx Linux - Software 14 11-01-2003 09:47 PM
Help a newbie who wants to learn ... please linux_latino Linux - Security 3 10-17-2003 12:13 PM
absolutely newbie, want to learn CLI chasw98 Linux - Newbie 1 08-19-2002 06:26 PM
How does a Newbie learn Terminal? tomplate Linux - Newbie 13 03-01-2002 03:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration