Just started C++..need help

This is a discussion on Just started C++..need help within the C++ Programming forums, part of the General Programming Boards category; i combined most of the example codes and made one that works and doesn't just turn off immediately. However, i ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Unhappy Just started C++..need help

    i combined most of the example codes and made one that works and doesn't just turn off immediately. However, i made a new file and it turns off as soon as it runs. everything is pretty much the same though..i don't know what i'm doing wrong. this is the code for the program that stays up until i'm finished with it:
    Code:
     
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int thisisanumber;
      int a;
      int age;
      a = 4 * 5;
      cout<<"Answer is: " << a <<endl;
      cout<<"HEY, you, I'm alive! Oh, and Hello World!" <<endl;
      cout<<"Enter a number: "<<endl;
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered: "<< thisisanumber << endl;
      
      cout<<"Please input your age: "<<endl;
      cin>> age;
      cin.ignore();
      if ( age < 100 ) {
         cout<<"You are pretty young!"<<endl;
      }
      else if ( age == 100 ) {
         cout<<"You are old"<<endl;
      }
      else {
        cout<<"You are really old"<<endl;
      }
      cin.get();
      
      return 0;
    }

    OK..now this is the code of the much smaller program that just wont work...whats wrong with it???
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     
      for ( int x = 0; x < 10; x++ ) {
        cout<< x <<endl;
      }
    return 0;
    }
    Please help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I would guess by the title that there is nothing wrong with it. You may be running it from an environment in which a window is spawned, the program executes, and when finished closes.

    Run command line stuff from the command line, perhaps. Or else investigate the FAQ for IDE workarounds.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Welcome to the boards.

    It opens runs and closes, thats because there is nothing to keep it open.

    A)Run it from a command window(in windows)
    B)Add a cin.get() at the end before return 0(this will wait for enter to be pressed).

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    i use dev C++...
    i added the cin.get() but now i get a couple errors on the bottom...(these are the errors)

    --multiple definition of main,

    --first defined here,

    --ld returned 1 exit status,

    --C:\Dev-Cpp\Makefile.win [Build Error] [Project1.exe] Error 1

    ...which i have no idea what they mean :-/
    how would i run it from a command window?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by gtheripper01 View Post
    how would i run it from a command window?
    Open a command shell. Execute the program at the command line.

    How GUI dependent are you? [No insult, merely a probing question.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    i know that gui means graphical user interface.. and thats about it lol. (im assuming you mean using the command prompt. am i right?)
    well i looked into DOS a while back but not enough to really understand it too clearly.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Oh and if anyone is familiar with dev c++, can you explain the 4 error s i mentioned previously?
    i seem to get them a lot and don't know how to fix them.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For me in XP: Stert->All Programs->Accessories->Command Prompt

    Then navigate to the directory containing your executable and then execute.

    RE: the error -- post the latest please.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 08:51 PM
  2. Help getting started :(
    By blackocellaris in forum C Programming
    Replies: 4
    Last Post: 11-05-2006, 05:50 PM
  3. Getting started
    By panzeriti in forum Game Programming
    Replies: 3
    Last Post: 06-28-2003, 10:13 AM
  4. How to get started?
    By anoopks in forum Linux Programming
    Replies: 0
    Last Post: 01-14-2003, 02:48 AM
  5. Need help getting started
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2001, 11:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21