Thread: local function definitions are illegal?? What the...??

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    local function definitions are illegal?? What the...??

    What does this mean? It says that in the functions "verOne" and "verTwo". What is going on here. (I am using MSVC++ 6.0) Here is my code:



    // calculates how long you would have to save for
    // by asking for various information and working from that

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>

    long double amount = 0;
    long double earn = 0;
    long double percent = 0;
    int weeks = 0;
    int result = 0;

    void verOne()
    {
    // get info
    system("cls");
    cout << "Enter the amount of money that you would like to save:$";
    cin >> amount;
    cout << "\n";

    cout << "Enter the amount of money that you earn each week:$";
    cin >> earn;
    cout << "\n";

    cout << "Enter the percentage of your weekly earnings that you would\n";
    cout << "like to save each week: ";
    cin >> percent;
    cout << "\n";

    // calculate end result
    percent = percent/earn * 100;
    result = amount/percent;

    if(result < 1)
    {
    cout << "It would take you 1 week to reach your target.\n";
    exit(0);
    }
    else
    {
    cout << "It would take you ";
    cout << result;
    cout << " week(s) to reach your target.\n";

    exit(0);
    }

    void verTwo()
    {
    //get info
    system("cls");
    cout << "Enter the amount of money that you would like to save:$";
    cin >> amount;
    cout << "\n";

    cout << "Enter the maximum amount of weeks that you have to save this money: ";
    cin >> weeks;

    // calculate result
    result = amount/weeks;
    cout << "You would have to save $";
    cout << result;
    cout << " each week for ";
    cout << weeks;
    cout << " weeks.";
    cout << "\n";

    exit(0);
    }

    int main()
    {
    int choice;
    cout << "Would you like to run version 1 or version 2? ";
    cin >> choice;

    switch(choice)
    {
    case 1:
    verOne();
    break;

    case 2:
    verTwo();
    break;
    }
    return 0;
    }
    }


    Thanks
    -Chris

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(result < 1)
    See this?

    Well the else part is missing a }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Talking

    You need to define the two functions that you want to use, just like you define the variable in your program

    Code:
    #include <iostream.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    
    long double amount = 0; // these are global vars. it is safer to
    long double earn = 0;     // move these inside of the main fun.
    long double percent = 0; // and pass b/n functions.
    int weeks = 0; 
    int result = 0; 
    
    
    void verOne(); // Define your functions too
    void verTwo(); // Define your functions too
    
    
    int main() 
    { 
    	int choice; 
    	cout << "Would you like to run version 1 or version 2? "; 
    	cin >> choice; 
    
    	switch(choice) 
    	{ 
    		case 1: verOne(); 
    				break; 
    
    		case 2: verTwo(); 
    				break; 
    	} 
    	return 0; 
    }
    
    void verOne() 
    { 
    	// get info 
    	system("cls"); 
    	cout << "Enter the amount of money that you would like to save:$"; 
    	cin >> amount; 
    	cout << "\n"; 
    
    	cout << "Enter the amount of money that you earn each week:$"; 
    	cin >> earn; 
    	cout << "\n"; 
    
    	cout << "Enter the percentage of your weekly earnings that you would\n"; 
    	cout << "like to save each week: "; 
    	cin >> percent; 
    	cout << "\n"; 
    
    	// calculate end result 
    	percent = percent/earn * 100; 
    	result = amount/percent; 
    
    	if(result < 1) 
    	{ 
    		cout << "It would take you 1 week to reach your target.\n"; 
    		exit(0); 
    	} 
    	else 
    	{ 
    		cout << "It would take you "; 
    		cout << result; 
    		cout << " week(s) to reach your target.\n"; 
    
    		exit(0); 
    	}
    }
    
    void verTwo() 
    { 
    	//get info 
    	system("cls"); 
    	cout << "Enter the amount of money that you would like to save:$"; 
    	cin >> amount; 
    	cout << "\n"; 
    
    	cout << "Enter the maximum amount of weeks that you have to save this money: "; 
    	cin >> weeks; 
    
    	// calculate result 
    	result = amount/weeks; 
    	cout << "You would have to save $"; 
    	cout << result; 
    	cout << " each week for "; 
    	cout << weeks; 
    	cout << " weeks."; 
    	cout << "\n"; 
    
    	exit(0); 
    }
    -----------------------------------------------
    everready

    To code, or not to code, that is the question.

    Well the answer is 'TO CODE' of cause

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. local function definitions are illegal
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 04:09 AM