Thread: Trouble running a program Visual Studio

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Trouble running a program Visual Studio

    Hi,

    I'm learning FOR loops and have made this code:-

    Code:
    using namespace std;
    
    int main()
    {
    int array1[2];
    int i = 0;
    int total = 0;
    int semitotal =0;
    
    for(i=0;i<3;i++)
    	{
    		cout << "Enter a number " << endl;
    		cin >> array1[i];
    	}
    	cout << "outside 1st FOR loop" << endl;
    
    for(i=0;i<2;i++)
    	{
    		cout << "number was" << array1[i] << endl;
    	}
    	cout << "outside 2nd FOR loop" << endl;
    	
    for(i=0;1<3;i++)
    	{
                     array1[i] = semitotal;
                     semitotal += total = total;
    	}
    	cout << "outside 3rd FOR loop" << endl;
    	cout << "array total is" << total << endl;
    
    	//cin.get();
    	system("pause");
    	
    	return 0;
    
    }
    When I build it in Visual Studio it always says the following:-

    The following breakpoint cannot be set:

    At int loopcount;

    Unable to parsethe function:int loopcount;
    So I installed Dev C++ and it runs the program, but the console window won't remain open despite trying both cin.get(); and system("pause"); .

    Could anyone offer me out with either these two issues?

    Many thanks.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm, for one your loops go out of bounds. As for the breakpoint, im guessing that is what it is referring to.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    for(i=0;1<3;i++)

    is that a typo or do you really want 1<3 as the condition?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Swerve View Post
    int loopcount;
    Well as a compiler I think I'd also find it hard setting a breakpoint on a line of code that doesn't exist, at least not in what you posted.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thank You to everyone for their advice, it is extremely appreciated!

    I have cleaned up my code:-

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int array1[2];
    int i = 0;
    int total = 0;
    int subtotal = 0;
    
    for(i=0;i<3;i++)
    	{
    		cout << "Enter a number " << endl;
    		cin >> array1[i];
    	}
    	cout << "outside 1st FOR loop" << endl;
    
    for(i=0;i<3;i++)
    	{
    		cout << "number was" << array1[i] << endl;
    	}
    	cout << "outside 2nd FOR loop" << endl;
    	
    for(i=0;i<3;i++)
    	{
    		subtotal += array1[i];
    	}
    	cout << "outside 3rd FOR loop" << endl;
    	cout << "array total is" << subtotal << endl;
    
    	//cin.get();
    	system("pause");
    	
    	return 0;
    
    }






    When the terminal window is closed, it says:-

    Run-Time Check Failure #2 - Stack around the variable 'array1' was corrupted.
    If anyone can offer me advice on the matter, I'd be very grateful.

    Thanks!
    Last edited by Swerve; 03-03-2008 at 10:58 AM.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    You still have the problem Raigne pointed out. You are looping outside your array's boundary. You have a 2 element array, but you are looping through : 0,1,2.

    Also :

    Code:
    for(i=0;i<3;i++)
    	{
    		array1[i] = subtotal;
                    subtotal += total = total;
    	}
    you don't need the variable subtotal. And subtotal += total = total isn't doing want you want.

    Try
    Code:
    total += array[i];

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Now try changing your for loop : i<2;

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try selecting your code and pressing Alt+F8 to let Visual Studio indent for you. Simple as can be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thank you to you both!

    I thought that array[2] created an array of 3 segments (0,1 and 2), but now I know the '2' includes the 0 element.

    For loop fixed (assignment is right to left!).

    Many thanks


    EDIT - Elysia, that's a great tip. never knew you could do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Replies: 10
    Last Post: 04-07-2008, 09:14 AM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. SystemTray class
    By jair in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2003, 06:27 PM