hey i have a problem with adding an is numeric checker to my code for my class when i hit a snag just wondering if i could get some help

this is my first program so help is appreciated

// fun with physics.cpp : Defines the entry point for the console application.



//INCLUDES
#include "stdafx.h"
#include <iostream>
using namespace std;

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include "math.h"
#include <iomanip>

//FUNCTIONS


//MAIN
int main()
{

int isnum(const char *p) 
{
     if (*p) {
          char c;
          while ((c=*p++)) {
                if (!isdigit(c)) return 0;
          }
          return 1;
      }
      return 0;
}


bool finished = false;   
while(finished==false)
{
	

cout << "\n What equation do u want to use?";
cout << "\n1. Quadratic Equation";
cout << "\n2. Distance Formula";
cout << "\n3. Velocity Formula";
cout << "\n4. Acceleration Formula";
cout << "\n5. Dividing With Error";
cout << "\n6. RMS";
cout << "\n7. Squaring Error";
cout << "\n8. Multiplying With Error";
cout << "\nEnter the number of the program you wish to use!\n";

int o;

cin >> o;
if (o == 1)
{
	cout << "Quadratic equation\n";
    cout << "enter a: ";
    float a;
    cin >> a;
	
	cout << "Enter b: ";
    float b;
	cin >> b;
	
	cout << "Enter c: ";
    float c;
	cin >> c;
	
	if (isnum(a)&& isnum
		(b) && isnum(c) && a!=0) {
	float z = pow (b,2);
	

	float x = z - (4* a *c);
	
	
	float y = sqrt (x);
	

	float d= ( (-b + y) / (2*a));
	float e= ( (-b - y) / (2*a));
	
	cout << "\nYour answer is\n";
	cout << d;
	cout << "\n or \n";
	cout << e;
	cout << "\n";
}
	else { cout << " please check your input\n";
	}
}
if (o == 2)
 {
	cout << "Distance\n";
	cout << "Enter Final Velocity:";
	double x;
	cin >> x;
	
	cout << "Enter Initial Velocity:";
	double y;
	cin >> y;
	
	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x+y)/2)*t;
	
	cout << "Distance is\n";
	cout <<"\n";
	cout << d;
}
if (o == 3)
{
	cout << "Velocity\n";
	cout << "Enter Final Distance:";
	double x;
	cin >> x;

	cout << "Enter Initial Distance:";
	double y;
	cin >> y;

	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x-y)/t);
	
	cout << "Velocity is\n";
	cout << d;
	cout << "\n";
}
if (o == 4)
{
	cout << "Acceleration\n";
	cout << "Enter Final veloctiy:";
	double x;
	cin >> x;

	cout << "Enter Initial velocity:";
	double y;
	cin >> y;

	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x-y)/t);
	cout << "Acceleration is\n";
	cout << d;
}
if (o == 5){
	cout << "Enter A then error1\n";
	float a;
	cin >> a;
	float e;
	cin >> e;
	
	cout << "Enter B then error2\n";
	float b;
	cin >> b;
	float f;
	cin >> f;
	float z = a / b;
	float y = ( e / a) * 100;
	float ysquared = pow ( y, 2);
	float p = (f / b) * 100;
	float psquared = pow ( p, 2);
	float c = (ysquared + psquared);
	float i = sqrt (c);
	float x = i / 100;
	float q = z * x;
	cout << "Your answer is\n" << z << "\n+ or -\n" << q;
	cout << "\n";
}
if (o == 6)
{
	cout << "Input x1 through x3.\n";
	float b;
	cin >> b;
	float c;
	cin >> c;
	float d;
	cin >> d;

	float a;
	a =((b + c + d) / 3);
	
	float y =(b - a);
	float x = pow (y,2);
	float w = (c - a);
	float z = pow (w,2);
	float p = (d - a);
	float j = pow (p,2);
	float q = (x + z + j) / (6);

	float T = sqrt (q);
	cout << "Your answer is\n"<< a;
	cout << "\n plus or minus \n";
	cout << T <<"\n";

}
if (o == 7){
	cout << "Enter number\n";
	float a;
	cin >> a;
	
	cout << "Enter error\n";
	float e;
	cin >> e;
	float b = pow (a,2);
	float y = b * (2*(e/a));

cout << "Your answer is \n";
cout << b;
cout << "\n+ or -\n";
cout << y;
}
if (o == 8){
cout << "Enter first number\n";
float a;
cin >> a;
cout << "Enter error\n";
float b;
cin >> b;
cout << "Enter second number\n";
float c;
cin >> c;
cout << "Enter error\n";
float d;
cin >> d;
float e = (100*b/a);
float f = (100*d/c);
float z = pow (e,2);
float x = pow (f,2);
float y = z + x;
float v = sqrt (y);
float s = a * c; 
cout << s;
cout << "\nwith a precent error of\n";
cout << v;
}
char tempChar;    
	cout << "Do you wish start the program again? [Y/N]: ";    
	cin >> tempChar; 

		if( tempChar == 'N' || tempChar == 'n' )      			
		finished = true;
}

return 0;
}

errors:
------ Build started: Project: fun with physics, Configuration: Release Win32 ------
Compiling...
fun with physics.cpp
.\fun with physics.cpp(25) : error C2601: 'isnum' : local function definitions are illegal
.\fun with physics.cpp(22): this line contains a '{' which has not yet been matched
.\fun with physics.cpp(71) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
.\fun with physics.cpp(72) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
.\fun with physics.cpp(72) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
Build log was saved at "file://\\nasserver2\cgrealish11\Documents\Visual Studio 2008\Projects\fun with physics\fun with physics\Release\BuildLog.htm"
fun with physics - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

WaltP commented: First post -- CODE tags *and* Formatted code! Fantastic! +9

Recommended Answers

All 11 Replies

> .\fun with physics.cpp(25) : error C2601: 'isnum' : local function definitions are illegal
This says it all.
Move your function definition OUTSIDE main.

You cannot define a function within main() . Move it above.

And thank you for using CODE tags and formatted code on your first post! What a rare thing around here!

> .\fun with physics.cpp(25) : error C2601: 'isnum' : local function definitions are illegal
This says it all.
Move your function definition OUTSIDE main.

thank you
that took care of the first error
but i am still having an issue with using the function

thank you
that took care of the first error
but i am still having an issue with using the function

My psychic powers aren't awake yet...

now i am getting the errors:
------ Build started: Project: fun with physics, Configuration: Release Win32 ------
Compiling...
fun with physics.cpp
.\fun with physics.cpp(70) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
.\fun with physics.cpp(71) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
.\fun with physics.cpp(71) : error C2664: 'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
Build log was saved at "file://\\nasserver2\cgrealish11\Documents\Visual Studio 2008\Projects\fun with physics\fun with physics\Release\BuildLog.htm"
fun with physics - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


here is the code:

// fun with physics.cpp : Defines the entry point for the console application.



//INCLUDES
#include "stdafx.h"
#include <iostream>
using namespace std;

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include "math.h"
#include <iomanip>

//FUNCTIONS
int isnum(const char *p) 
{
     if (*p) {
          char c;
          while ((c=*p++)) {
                if (!isdigit(c)) return 0;
          }
          return 1;
      }
      return 0;
}


//MAIN
int main()
{


bool finished = false;   
while(finished==false)
{
	

cout << "\n What equation do u want to use?";
cout << "\n1. Quadratic Equation";
cout << "\n2. Distance Formula";
cout << "\n3. Velocity Formula";
cout << "\n4. Acceleration Formula";
cout << "\n5. Dividing With Error";
cout << "\n6. RMS";
cout << "\n7. Squaring Error";
cout << "\n8. Multiplying With Error";
cout << "\nEnter the number of the program you wish to use!\n";

int o;

cin >> o;
if (o == 1)
{
	cout << "Quadratic equation\n";
    cout << "enter a: ";
    float a;
    cin >> a;
	
	cout << "Enter b: ";
    float b;
	cin >> b;
	
	cout << "Enter c: ";
    float c;
	cin >> c;
	
	if (isnum(a)&& isnum
		(b) && isnum(c) && a!=0) {
	float z = pow (b,2);
	

	float x = z - (4* a *c);
	
	
	float y = sqrt (x);
	

	float d= ( (-b + y) / (2*a));
	float e= ( (-b - y) / (2*a));
	
	cout << "\nYour answer is\n";
	cout << d;
	cout << "\n or \n";
	cout << e;
	cout << "\n";
}
	else { cout << " please check your input\n";
	}
}
if (o == 2)
 {
	cout << "Distance\n";
	cout << "Enter Final Velocity:";
	double x;
	cin >> x;
	
	cout << "Enter Initial Velocity:";
	double y;
	cin >> y;
	
	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x+y)/2)*t;
	
	cout << "Distance is\n";
	cout <<"\n";
	cout << d;
}
if (o == 3)
{
	cout << "Velocity\n";
	cout << "Enter Final Distance:";
	double x;
	cin >> x;

	cout << "Enter Initial Distance:";
	double y;
	cin >> y;

	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x-y)/t);
	
	cout << "Velocity is\n";
	cout << d;
	cout << "\n";
}
if (o == 4)
{
	cout << "Acceleration\n";
	cout << "Enter Final veloctiy:";
	double x;
	cin >> x;

	cout << "Enter Initial velocity:";
	double y;
	cin >> y;

	cout << "Enter Time:";
	double t;
	cin >> t;
	double d;
	d = ((x-y)/t);
	cout << "Acceleration is\n";
	cout << d;
}
if (o == 5){
	cout << "Enter A then error1\n";
	float a;
	cin >> a;
	float e;
	cin >> e;
	
	cout << "Enter B then error2\n";
	float b;
	cin >> b;
	float f;
	cin >> f;
	float z = a / b;
	float y = ( e / a) * 100;
	float ysquared = pow ( y, 2);
	float p = (f / b) * 100;
	float psquared = pow ( p, 2);
	float c = (ysquared + psquared);
	float i = sqrt (c);
	float x = i / 100;
	float q = z * x;
	cout << "Your answer is\n" << z << "\n+ or -\n" << q;
	cout << "\n";
}
if (o == 6)
{
	cout << "Input x1 through x3.\n";
	float b;
	cin >> b;
	float c;
	cin >> c;
	float d;
	cin >> d;

	float a;
	a =((b + c + d) / 3);
	
	float y =(b - a);
	float x = pow (y,2);
	float w = (c - a);
	float z = pow (w,2);
	float p = (d - a);
	float j = pow (p,2);
	float q = (x + z + j) / (6);

	float T = sqrt (q);
	cout << "Your answer is\n"<< a;
	cout << "\n plus or minus \n";
	cout << T <<"\n";

}
if (o == 7){
	cout << "Enter number\n";
	float a;
	cin >> a;
	
	cout << "Enter error\n";
	float e;
	cin >> e;
	float b = pow (a,2);
	float y = b * (2*(e/a));

cout << "Your answer is \n";
cout << b;
cout << "\n+ or -\n";
cout << y;
}
if (o == 8){
cout << "Enter first number\n";
float a;
cin >> a;
cout << "Enter error\n";
float b;
cin >> b;
cout << "Enter second number\n";
float c;
cin >> c;
cout << "Enter error\n";
float d;
cin >> d;
float e = (100*b/a);
float f = (100*d/c);
float z = pow (e,2);
float x = pow (f,2);
float y = z + x;
float v = sqrt (y);
float s = a * c; 
cout << s;
cout << "\nwith a precent error of\n";
cout << v;
}
char tempChar;    
	cout << "Do you wish start the program again? [Y/N]: ";    
	cin >> tempChar; 

		if( tempChar == 'N' || tempChar == 'n' )      			
		finished = true;
}

return 0;
}

>'isnum' : cannot convert parameter 1 from 'float' to 'const char *'
Why yes, a float is indeed not the same as a string. You can take input as a string rather than a float to correct the problem. Alternatively you can convert the float to a string, in which case isnum becomes superfluous because cin requires input to be in the correct format for the requested type to begin with.

This function can be more simple :

int isnum(const char *p) 
{
     if (*p) {
          char c;
          while ((c=*p++)) {
                if (!isdigit(c)) return 0;
          }
          return 1;
      }
      return 0;
}

Maybe to this ?

bool isDigits(const char * strNum){
 while(*strNum){
        if(isdigit(*strNum++) ) continue;
        else return false;
  }
 return true;
}

You use isnum function here :

if (isnum(a)&& isnum
		(b) && isnum(c) && a!=0)

but the variable a, b, and c are of type float? Perhaps consider
making it of type char *, then convert it to a number ?

i couldn't figure out a easy way to convert a float to a char *

although i did find cin.fail() )
but i figured out it doesn't work with floats only with int
so i was wondering if anyone has an suggestions on where as to go from this point on or could explain how to use cin.fail or how to convert a float to a char* or any other way that could possibly solve these problems

>i couldn't figure out a easy way to convert a float to a char *
As I said, if you read the input as float, cin will handle the formatting for you and fail completely if it's invalid. If you get to the point where you want to convert to a string and call isnum (which is an extremely poor name, btw, because it invades the implementation name space), the function is completely redundant because the float is already guaranteed to be legit.

>although i did find cin.fail() )
>but i figured out it doesn't work with floats only with int

How do you figure that? Whether (and how) cin fails is independent of the type you requested. It even works for unformatted requests. The following is a basic loop for reading input and checking for errors:

#include <ios>
#include <iostream>
#include <limits>

int main()
{
  float f;
  
  std::cout<<"Enter a float: ";

  while ( !( std::cin>> f ) ) {
    std::cin.clear();
    std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Invalid input, please try again: ";
  }

  std::cout<<"You entered "<< f <<'\n';
}

>could explain how to use cin.fail or how to convert a float to a
>char* or any other way that could possibly solve these problems

I already explained the best way. You clearly want to use isnum, which means you want to parse and validate a line of input, so you should be taking input as a string:

std::string a;

if ( std::getline ( std::cin, a ) && isnum ( a.c_str() ) )
  std::cout<<"Woohoo!\n";

From what I see you are doing :

cout << "Quadratic equation\n";
    cout << "enter a: ";
    float a;
    cin >> a;
 
	cout << "Enter b: ";
    float b;
	cin >> b;
 
	cout << "Enter c: ";
    float c;
	cin >> c;
 
	if (isnum(a)&& isnum
		(b) && isnum(c) && a!=0) {
	float z = pow (b,2);
 
	float x = z - (4* a *c);

Do this instead :

string a , b, c;
	cout << "Enter a : ";
	cin >> a;
	cout << endl;
	cout << "Enter b : ";
	cin >> b;
	cout << endl;
	cout << "Enter c : ";
	cin >> c;
	cout << endl;
	
	if (isDigits(a.c_str())&& isDigits
		(b.c_str()) && isDigits(c.c_str()) && !a.empty()) {
		
	float z = pow (atof(b.data()),2.0);
  
	float x = z - (4.0* atof(a.data()) * atof(a.data()));

The main this there is the use of std::string which resides in the
string library, i.e #include<string> .

The other main thing is the use of atof, although not the best solution,
it is the simplest. It convert a char* to a number if possible.
For instance,

float distance = atof("24.001"); //distance will equal 24.001

Damn Narue, your a Ninja; Thats another post you beat me on.

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.