Problem - not sure what to try next...

This is a discussion on Problem - not sure what to try next... within the C++ Programming forums, part of the General Programming Boards category; Hello, Like many people on these boards I am new to C++ programming, and also like many, I have come ...

  1. #1
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118

    Problem - not sure what to try next...

    Hello,

    Like many people on these boards I am new to C++ programming, and also like many, I have come across a problem for which I'm having difficulty finding the answer.

    I've had a check around the boards and other sites and it seems quite a familiar problem - the one whereby a program closes before you get to see the result. Well I'm having a similar problem.

    After reading a few tutorials I decided to try to write a program which computes powers of integers. The user enters 'a' and 'b' and the program will return a^b. I thought about using a for loop to do this. Here is what I came up with

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main(){
        
        int val;
        int pow;
        int res = 1;
        
        cout << "Please enter your number: ";
        cin >> val;
        cin.ignore();
        cout << "Please enter your power: ";
        cin >> pow;
        cin.ignore();
        
        for(int i=0; i<pow; i++){
                res = res * val;
                return res;
                }
                
                cout << "The answer is " << res;
                getch();
                return 0;
                }
    Now the program lets the user enter the number and the power but once you hit return to get the result...poof!...the program closes. I have tried solutions such as cin.get() and getch() but to no avail. I would greatly appreciate some help on the matter. Even if you only tell me where I'm going wrong.

    Thanks in advance

    Stonehambey

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure about the return statement in the middle of main?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Also suggest you don't place your } like that. That will only cause confusion and brings bugs.

    Code:
    	for(int i=0; i<pow; i++){
    		res = res * val;
    		return res;
    	}
                
    	cout << "The answer is " << res;
    	getch();
    	return 0;
    }
    Also keep indentation consistent. Use X number of spaces (typically 4) or a tab for each level to indent. Cout & co should be on the same line as the for's ending }. This makes the code more readable.

    But as anon implies, the return in the loop is doing something you don't want it to, can you figure out what?
    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.

  4. #4
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    Quote Originally Posted by Elysia View Post
    Also suggest you don't place your } like that. That will only cause confusion and brings bugs.

    Code:
    	for(int i=0; i<pow; i++){
    		res = res * val;
    		return res;
    	}
                
    	cout << "The answer is " << res;
    	getch();
    	return 0;
    }
    Also keep indentation consistent. Use X number of spaces (typically 4) or a tab for each level to indent. Cout & co should be on the same line as the for's ending }. This makes the code more readable.
    Good idea. I have experience with html so I should've known better! Guess I was just being lazy, not a good start if I want to write more complicated programs :P

    But as anon implies, the return in the loop is doing something you don't want it to, can you figure out what?
    Hehe, well I'm very new to this but I will have a dig and see if I can figure it out. If not I may come back for another hint. Thanks for the kind replies

    Stonehambey

    Edit Well I simply took the return out of the program and it worked! Not entirely sure why yet but I'm sure it'll occur to me as I learn more about the language...
    Last edited by Stonehambey; 01-19-2008 at 06:46 AM.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The RETURN says to exit the current function to return to the caller. In this case, the currently executing function was main(), and the caller was the operating system. Only when main() is finished processing do you want to return.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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