Well, apparently, I jumped the gun a little.
My code for the program I wrote still doesn't work.
It compiles fine and throws no errors, but when I run it, only my main menu list runs-- over and over, regardless of my input, and the esc key no longer works.

I have a list of three options.
1- func_A
2- func_B
3- func_C

I've used switch up until now, and as stated previously, it ran fine. This evening I tried multiple nested if else if statements, and obtained the same issue. My console run just output my primary menu-- over and over again, regardless of my input.

Below is my code.

float hipvalley(float argc, char *argv[]); 
float cricketvalley(float argc, char *argv[]);
float ridgeangle(float argc, char *argv[]);
//I've tried other terms within the parenthesis for the above functions, and 
// a variety of errors are thrown. They are briefly discussed below.

int _tmain(int argc, _TCHAR* argv[])
{
do
     {
         cout << "Copyright 2001, Welcome to the Angle Finder Program." << endl;
         cout << "This program is designed to take only numeric values." << endl;
         cout << "Make certain you only input numbers. Otherwise it will exit." << endl;
		cout << " Please choose:  1 for the Hip/Valley Angle. "<< endl;
		cout << "                 2 for the Cricket Valley Angle. " << endl;
		cout << "                 3 for the Ridge Angle " << endl;
		cout << "                 ESC to exit. "<< endl;
		ch = getchar();
				
		switch (ch)
		{ 
		case 1 : float hipvalley(); // this is the Hip/Valley choice.
		break;
		//first run through resulted in the program
		// just repeating the same intro language
		// over and over again, regardless of my input.
		//Find out what's needed to make this switch
		//statement work in here.
                               // same issue after 20 or 30 runs. repeated intro menu...regardless
                              //  of input.
		case 2 : float cricketvalley(); // this is the cricket valley choice.
			break;
		case 3 : float ridgeangle(); // this is the Ridge Angle choice.
		if (ch != 27) cout << " Sorry, that is not a valid choice!  " << endl;
		}
}     while (ch != 27);
return(0);

What am I missing here?
At this point, I'm honestly thinking there is something wrong with how I've defined my functions above-- hipvalley(), cricketvalley(), and ridgeangle().
I did just change the contents within the () to (void), from (float argc, char *argv[]), and received the same results. repeating main menu.
I changed the float to int, and same result.
I then removed int entirely from all occurrences, and got errors-- same as before-- C++ cannot assume int......
So, I've replaced int in front of the functions, and got the same results as listed above-- my main menu is the only output.
Any help is deeply appreciated.
Best.

Recommended Answers

All 12 Replies

your switch is sensitive to a char, but your cases are ints.

it's been half a year since i last used c++, but if i'm not mistaken, you should be using case '1': , case '2': , etc. case 1: would be if the user entered in a bell sound or something :P

case 1 : float hipvalley(); // this is the Hip/Valley choice.

that is merely a declaration of the function hipvalley.
to invoke the function (and ignore its result), write

case '1' : hipvalley(); // this is the Hip/Valley choice.
#include <iostream>

using namespace std;// You forgot your include files, as well as your namespace


float hipvalley();
float cricketvalley();
float ridgeangle();

int main()
{
cout << "Copyright 2001, Welcome to the Angle Finder Program." << endl
 << "This program is designed to take only numeric values." << endl
 << "Make certain you only input numbers. Otherwise it will exit." << endl
<< " Please choose: 1 for the Hip/Valley Angle. "<< endl
 << " 2 for the Cricket Valley Angle. " << endl
 << " 3 for the Ridge Angle " << endl
 << " 0 to exit. "<< endl;

for(;;){ //You had do/while, I think it's easier to just do a for() loop
         // where one of your choices will break the loop
int ch;
cin >> ch;

switch(ch)
{
	case 1 : 
 		
  		hipvalley(); // this is the Hip/Valley choice.
 		break;

	case 2 :
 		 cricketvalley(); // this is the cricket valley choice.
 		 break;
	
 	case 3 : 
  		ridgeangle(); 
  		break;
    
    case 0: //Since ch is an int, I'm just going with 0 here
    cout << " Sorry, that is not a valid choice! " << endl;
    cin.get();
    return 0; // returns main(), should exit the for loop
}
}
}

I commented a few of the changes I made. Now it's a for loop around your input selection instead of a do/while. I'm not sure what you want to accomplish with your functions, but depending on what case you use it'll execute the one you want. As for the arguments you were passing to the function, again I don't know what you want to accomplish.

As for the menu, it won't repeat anymore, but only because your functions aren't doing anything yet.

Er, hope that helps maybe a little.

ICorey,
I had first noticed my issue when the '1' single quote marks were on the numbers in my switch list. I had removed them, and it did not resolve it. I then tried multiple items, and just never repalced them.

Ellisande,
As to my choice of do-while.
At the time I'd originally made this-- 1999-- I liked (I should say that I understood it better than the 'for') the do while, and didn't really understand the for.
Now, as to the for loop. I don't ever remember using the semi-colon inside of a for before.
I remember
for (i>1; i++; i<N) where N was some numeric value of my choosing.
But here you have just two semi-colons.
for (;;)
Does that perform some task? Or did you make it that way deliberately?


As to my functions, they are mathematical constructs to solve an old problem that I was once faced with some 13 years ago.

Thanks for your respective inputs, I'll apply your solutions, and see what I come up with.
Best.

ICorey,
I had first noticed my issue when the '1' single quote marks were on the numbers in my switch list. I had removed them, and it did not resolve it. I then tried multiple items, and just never repalced them.

Ellisande,
As to my choice of do-while.
At the time I'd originally made this-- 1999-- I liked (I should say that I understood it better than the 'for') the do while, and didn't really understand the for.
Now, as to the for loop. I don't ever remember using the semi-colon inside of a for before.
I remember
for (i>1; i++; i<N) where N was some numeric value of my choosing.
But here you have just two semi-colons.
for (;;)
Does that perform some task? Or did you make it that way deliberately?


As to my functions, they are mathematical constructs to solve an old problem that I was once faced with some 13 years ago.

Thanks for your respective inputs, I'll apply your solutions, and see what I come up with.
Best.

for(;;) is an infinite loop. Ellisande ends the loop by exiting the program with return 0 .

Hi again.
Ok,
I applied the for loop.
I've also applied the corrections to my switch function.
I notice a couple of items up front, so please allow me to deal with them first.
The first thing that occurs when I run the console appl on this is that I get a blank cmd console.
I tried a a few keystrokes to get a response, and nothing.
I inadvertantly hit my space bar and that's when my main menu comes up.
I then pick a numeric value, and nothing happens.
So I closed it out, and recompile it.
I then clicked my spacebar again and it calls the menu.
This time my numeric entries only call the menu- as before.
Also, 0 does not call for the exit of the routine. It calls to my cout, which says this was not a valid choice....
I've recompiled about a dozen times now, and while my main menu now comes up without having to click my spacebar, no numeric value activates my functions.

Below is my code:

# include "stdafx.h"
# include <iostream>
# include <cmath>
# include <xutility>
# include <stdlib.h>
# include <cctype>
# include <conio.h>
# define PI 3.1415926535898


using namespace std;


//this is trial version 3 to update, and continue to test my programming skills.
// I did this once, let's see it happen again. 

double Rise1 = 0; //for vertical data input of 1st roof slope-- Rise1
double Rise2 = 0; //for vertical data input of 2nd roof slope-- rise
double Eave = 0; //for data input- eave orientation angle-- 0 through 180,
		 // 181 through 360 is identical to 0 through 180.
double a; //input data conversion for both cricket, and hip/valley routine.
		 // cross product angle equation.
double Width = 0; //data input for cricket routine-  cricket width.
char  ch1, ch2, ch3; //character choices


float atan(float);
float acos(float);
float sqrt(float);

int hipvalley (void);//(float argc, char *argv[]); 
int cricketvalley(void);//(float argc, char *argv[]);
int ridgeangle(void);//(float argc, char *argv[]);


int _tmain(int argc, _TCHAR* argv[])
{
	
	for(;;) //this is an infinite loop. One of the choices
		    // will end it.
	{
		cout << "         Copyright 2001, Welcome to the Angle Finder Program.          " << endl;
		cout << "        This program is designed to take only numeric values.          " << endl;
		cout << "       Make certain you only input numbers. Otherwise it will exit.    " << endl;
		cout << " Please choose:  1 for the Hip/Valley Angle. "         << endl;
		cout << "                 2 for the Cricket Valley Angle. "     << endl;
		cout << "                 3 for the Ridge Angle "               << endl;
		cout << "                 0 to exit. "                        << endl;
		int ch;
		cin >> ch;
		
		switch (ch)
		{ 
		case '1' : int hipvalley(); //(float argc, char *argv[]) this is the Hip/Valley choice.
		break;

		case '2' : int cricketvalley(); // (float argc, char *argv[])this is the cricket valley choice.
		break;

		case '3' : int ridgeangle(); //(float argc, char *argv[]) this is the Ridge Angle choice.
			
        break;
		case '0' : end (0); // this will end the routine.
			//cout << " Sorry, that is not a valid choice!  " << endl;
                                   //I tried ending the routine, and this did not work. 
		}
	} 
return (0);

no numeric value activates my functions.

Two changes ...

# include "stdafx.h"
# include <iostream>
# include <cmath>
# include <xutility>
# include <stdlib.h>
# include <cctype>
# include <conio.h>
# define PI 3.1415926535898
using namespace std;

double Rise1 = 0; //for vertical data input of 1st roof slope-- Rise1
double Rise2 = 0; //for vertical data input of 2nd roof slope-- rise
double Eave = 0; //for data input- eave orientation angle-- 0 through 180,
		 // 181 through 360 is identical to 0 through 180.
double a; //input data conversion for both cricket, and hip/valley routine.
		 // cross product angle equation.
double Width = 0; //data input for cricket routine-  cricket width.
char  ch1, ch2, ch3; //character choices


float atan(float);
float acos(float);
float sqrt(float);

int hipvalley (void);//(float argc, char *argv[]); 
int cricketvalley(void);//(float argc, char *argv[]);
int ridgeangle(void);//(float argc, char *argv[]);


int main()
{
	
	for(;;) //this is an infinite loop. One of the choices
		    // will end it.
	{
		cout << "         Copyright 2001, Welcome to the Angle Finder Program.          " << endl;
		cout << "        This program is designed to take only numeric values.          " << endl;
		cout << "       Make certain you only input numbers. Otherwise it will exit.    " << endl;

		cout << " Please choose:  1 for the Hip/Valley Angle. "         << endl;
		cout << "                 2 for the Cricket Valley Angle. "     << endl;
		cout << "                 3 for the Ridge Angle "               << endl;
		cout << "                 0 to exit. "                        << endl;

		char ch;
		cin >> ch;
		
		switch (ch)
		{ 
		case '1' : int hipvalley(); //(float argc, char *argv[]) this is the Hip/Valley choice.
		break;

		case '2' : int cricketvalley(); // (float argc, char *argv[])this is the cricket valley choice.
		break;

		case '3' : int ridgeangle(); //(float argc, char *argv[]) this is the Ridge Angle choice.			
        break;

		case '0' : return 0; // this will end the routine.
		}
	} 

	return (0);
}

If you use int ch; cin >> ch; then your case statements must be ..

case 1: ...
case 2: ...
etc

If you use char ch; cin >> ch; then your case statements must be ..

case '1': ...
case '2': ...
etc

You must not use ... case '1' : int hipvalley(); int hipvalley() is there a function declaration, which is not what you want. Instead, to invoke the function hipvalley(), use case '1' : hipvalley(); The same applies to the other two cases also.

Hi again.
Again, thank you for your respective inputs. They are deeply appreciated.

In response to the last post, I did as you recommended.

# include "stdafx.h"
# include <iostream>
# include <cmath>
# include <xutility>
# include <stdlib.h>
# include <cctype>
# include <conio.h>
# define PI 3.1415926535898


using namespace std;


//this is trial version 3 to update, and continue to test my programming skills.
// I did this once, let's see it happen again. 

double Rise1 = 0; //for vertical data input of 1st roof slope-- Rise1
double Rise2 = 0; //for vertical data input of 2nd roof slope-- rise
double Eave = 0; //for data input- eave orientation angle-- 0 through 180,
		 // 181 through 360 is identical to 0 through 180.
double a; //input data conversion for both cricket, and hip/valley routine.
		 // cross product angle equation.
double Width = 0; //data input for cricket routine-  cricket width.
char  ch1, ch2, ch3; //character choices


float atan(float);
float acos(float);
float sqrt(float);

int hipvalley (void); 
int cricketvalley(void);
int ridgeangle(void);


int _tmain(int argc, _TCHAR* argv[])
{
	
	for(;;) //this is an infinite loop. One of the choices
		    // will end it.
	{
	cout << "Copyright 2001, Welcome to the Angle Finder Program. " << endl;
	cout << "This program is designed to take only numeric values." << endl;
	cout << "Make certain you only input numbers. Otherwise it will exit." << endl;
	cout << " Please choose:  1 for the Hip/Valley Angle. "<< endl;
	cout << "                        2 for the Cricket Valley Angle. " << endl;
	cout << "                        3 for the Ridge Angle " << endl;
	cout << "                        0 to exit. "<< endl;
		char ch;
		cin >> ch;
		
		switch (ch)
		{ 
		case '1' :  hipvalley(); //this is the Hip/Valley choice.
		break;

		case '2' :  cricketvalley(); // this is the cricket valley choice.
		break;

		case '3' :  ridgeangle(); // this is the Ridge Angle choice.
		break;
		
                                case '0' : return 0; // this will end the routine.
			
		}
	} 
return (0);

I got the following errors

------ Build started: Project: SteveAngle, Configuration: Debug Win32 ------
Compiling...
stdafx.cpp
Compiling...
SteveAngle.cpp
Compiling manifest to resources...
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Copyright (C) Microsoft Corporation.  All rights reserved.
Linking...
SteveAngle.obj : error LNK2019: unresolved external symbol "int __cdecl ridgeangle(void)" (?ridgeangle@@YAHXZ) referenced in function _wmain
SteveAngle.obj : error LNK2019: unresolved external symbol "int __cdecl cricketvalley(void)" (?cricketvalley@@YAHXZ) referenced in function _wmain
SteveAngle.obj : error LNK2019: unresolved external symbol "int __cdecl hipvalley(void)" (?hipvalley@@YAHXZ) referenced in function _wmain
E:\Steve'sDocs\Visual Studio 2008\Projects\SteveAngle\Debug\SteveAngle.exe : fatal error LNK1120: 3 unresolved externals
Build log was saved at "file://e:\Steve'sDocs\Visual Studio 2008\Projects\SteveAngle\SteveAngle\Debug\BuildLog.htm"
SteveAngle - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

For some reason, it's as though my functions are calling to the outside of the cpp file.
However, my functions are down below.
Which was the ONLY reason why I'd tried placing the int in front of my function calls in the switch.

Below you have three function prototypes ..

int hipvalley (void); 
int cricketvalley(void);
int ridgeangle(void);

You need to implement them too, i.e.

// implementation of function hipvalley () ...
int hipvalley (void)
{
    int some_value;

    // some code here

    return some_value;
}

mitrmakr,
Thanks again for your response.
As I understand what you're saying,
AFTER i've declared my functions, with the int function_name(void)
I need to place the following in front of each routine which are the guts of my functions, correct?
// implementation of function hipvalley () ...
int hipvalley (void)
{
int some_value;

// my function code

return some_value;
}

Presently, my general post menu code is:

{
function_name1()

{
//my code for function_name1
//explanation menu

do

{

//my actual core code

}while(ch1!=27)

}//end of function


function_name2()
{
//my code for function_name2
//explanation menu

do

{
//my actual core code
}while(ch2 != 27)

} //end of function


function_name3()
{
//my code for function_name3
//explanation menu

do

{
//my actual core code

}while(ch3!=27)

} //end of function

}
return (0);

However, after trying my understanding of your fix, it still threw 3 errors, as previously stated.

Best.

First of all, please learn to use code tags.

It looked like you are maybe implementing the functions inside main(), which is not allowed. You cannot nest any functions, it is strictly forbidden, like for example

int somefunction()
{
    int some_value;

   void nestedfunction()
   {
        // nestedfunction()'s code here ...
       return;
   }
    return some_value;
}

You can also do without function prototypes in some cases, for example

int somefunction()
{
    // somefunction()'s code here ...
    return 0;
}
int main()
{
     // call somefunction() ..
     int i = somefunction();
     return i;
}

Or the same with a prototype ...

// prototype
int somefunction();
int main()
{
     // calling somefunction() at this point can be done
     // because the above prototype tells the compiler 
     // how to deal with somefunction() ...
     int i = somefunction();
     return i;
}
// implementation
int somefunction()
{
    // somefunction()'s code here ...
    return 0;
}

Hopefully you got the main idea how to organize your code.

mitrmkar,
sorry about the lack of code tags. It was late, and I was tired, I think I realized after I'd closed my computer for the night that I'd forgotten to set them.

First of all, please learn to use code tags.

It looked like you are maybe implementing the functions inside main(), which is not allowed. You cannot nest any functions, it is strictly forbidden, like for example

int somefunction()
{
    int some_value;

   void nestedfunction()
   {
        // nestedfunction()'s code here ...
       return;
   }
    return some_value;
}

This is new.... the old way functions would not work out of main. Of course that was close to 7 years ago.


You can also do without function prototypes in some cases, for example

int somefunction()
{
    // somefunction()'s code here ...
    return 0;
}
int main()
{
     // call somefunction() ..
     int i = somefunction();
     return i;
}

Or the same with a prototype ...

// prototype
int somefunction();
int main()
{
     // calling somefunction() at this point can be done
     // because the above prototype tells the compiler 
     // how to deal with somefunction() ...
     int i = somefunction();
     return i;
}
// implementation
int somefunction()
{
    // somefunction()'s code here ...
    return 0;
}

Hopefully you got the main idea how to organize your code.

So, it appears that from this point forward, I have my functions outside of the main. I was beginning to wonder about this last night as I was posting this, but it was more like a thought just dancing around the edges of an exhausted mind.
I'll give that a try tonight and let you know my results.
Thank you.
Best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.