Compiles okay, but doesn't run.

This is a discussion on Compiles okay, but doesn't run. within the C++ Programming forums, part of the General Programming Boards category; Hello, I've created this program and it all compiles correctly with no errors but once it has compiled it doesn't ...

  1. #1
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33

    Question Compiles okay, but doesn't run.

    Hello, I've created this program and it all compiles correctly with no errors but once it has compiled it doesn't run, it won't come up in command prompt (I have used Visual C++ to create this).

    Is there something in the code that could be stoping this or am I missing something?

    By the way I have had to put the
    Code:
    system ( "PAUSE" );
        return 0;
    cause before command prompt would only appear for a split second and disappear, I don't know whether or not that is effecting this program, but all I know is that I haven't been able to write a program without it,

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <assert.h>
    
    using namespace std;
    
    int main (int argc, char* argv[])
    {	
    	int mark [6];
    	int MarksRead=0;
    	int total=0;
    
    	if ( argc !=7)
    	{
    		cerr <<"Usage:./a.out n1 n2 n3 n4 n5 n6\n";
    			exit (1);
    	}
    	while (MarksRead <6)
    	{
    		cin>> mark [MarksRead];
    		MarksRead++;
    	}
    	for (int i=0; i<6; i++)
    	{
    		mark[i] =atoi(argv [i+1]);
    		assert(0 <= mark[i] && mark [i] <=100);
    	}
    	for (int i=0; i < 6; i++)
    	{
    		total += mark[i];
    	}
    	cout <<"Average Mark Is: "<<total/6.0 << endl;
      
    	system ( "PAUSE" );
        return 0;
      
    }
    any suggestions would be greatly appreciated, Karla.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try using cin.get() instead of system("PAUSE").
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Well, your program is waiting for your input. Isn't it?

    Code:
    	while (MarksRead <6)
    	{
    		cin>> mark [MarksRead];
    		MarksRead++;
    	}
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    still compiles okay, but hasn't come up in command prompt

  5. #5
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    Quote Originally Posted by Mario F.
    Well, your program is waiting for your input. Isn't it?

    Code:
    	while (MarksRead <6)
    	{
    		cin>> mark [MarksRead];
    		MarksRead++;
    	}
    yeah, Im quite confused on why it isnt working cause I orginally wrote this program at uni using emacs (without having to use the pause) and it worked fine but now on visual it doesn't.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You need to pass it arguments (n1, etc). Then, once you do that, it will wait for your input.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    In here?

    Code:
    int main (int argc, char* argv[])
    i'm still very new to c++ so i'm a bit slow
    Microsoft: "You have questions, we having dancing paperclips"

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No, on the command line. So try running it on the prompt (Run->cmd->*path_of_executable*), and passing it arguments there.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    sorry im still confused with what you mean?
    Microsoft: "You have questions, we having dancing paperclips"

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    You are trying to run the exectuable from the IDE, aren't you?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    yup.
    Microsoft: "You have questions, we having dancing paperclips"

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if ( argc !=7)
    	{
    		cerr <<"Usage:./a.out n1 n2 n3 n4 n5 n6\n";
    			exit (1);
    	}
    The name of the program is usually the first argument (I'm not sure if it is guaranteed, but I have never seen it otherwise.). If you pass 7 additional arguments, it will make 8.

    Code:
    while (MarksRead <6)
    	{
    		cin>> mark [MarksRead];
    		MarksRead++;
    	}
    	for (int i=0; i<6; i++)
    	{
    		mark[i] =atoi(argv [i+1]);
    		assert(0 <= mark[i] && mark [i] <=100);
    	}
    Why do you take user input in the first place, if you are going to overwrite it with command line values.
    assert is meant as a debugging tool to discover logical programming errors. Once the errors are fixed, this macro is usually disabled in the release build. User entering invalid parameters is not a programming error that you could fix, therefore you should test if the mark is in range using regular if.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Well, the executable is expecting you to pass it arguments before it is ran. VC++ should have an option to pass along command line arguments. However, I don't know where. So...

    You need to go to the command prompt (Start Menu->Run-> type cmd-> hit enter) and the navigate to your executable path. Then you type the executable name, a space, and six arguments separated by a space each. As in:

    "executable_name 1 2 3 4 5 6"

    Then you hit enter
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    I suggest anon you don't confuse her anymore.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    to be honest I don't even know how I wrote this, i just wrote this program based on the information given to me in lecture notes, so I don't really understand it fully.

    and I also don't know how to use command prompt so everything has just gone over my head lol, sorry i'm being really clueless but i've only been doing programming properly for just two months.
    Last edited by karlawarla; 11-23-2006 at 07:51 AM.
    Microsoft: "You have questions, we having dancing paperclips"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  2. My program compiles, but does not run
    By Surfer_Rosa in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:25 AM
  3. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  4. MSVC: run w/o debugger vs run w/ debuger
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 08:49 PM
  5. Replies: 2
    Last Post: 10-29-2002, 03:56 PM

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