The program I am doing has to have a static parseInt(const string& s)in it
I have tried different ways to do this but nothing work I get errors our teacher gave us a "clue" with this program:
static int MyInteger::parseInt (const string& s)
{
s.c_str();
return 1;
}
but I just don't get it I tried reading up on it but it is confusing for me help?!
Here is a copy of my program so far:
#include <iostream>
#include<cstdlib>
#include <sstream>
#include <string>
#include <stdlib.h>
#include "StackOfIntegers .h"
#include "MyInteger.h"
#include "Stock.h"
using namespace std;
//void Prog10_2();
//void Prog10_4();
//void Prog10_6();
//void Prog10_7();
void Prog10_10();
//void Prog10_12();
//void Prog10_14();
//static function test
cout << "n1 is " << n1.getValue() << " it is " << (MyInteger::isEven(13) ? "Even" : "Odd") << endl;
cout << "n1 is " << n1.getValue() << " it is " << (MyInteger::isOdd(13) ? "Even" : "Odd") << endl;
cout << "n1 is " << n1.getValue() << " it is " << (MyInteger::isPrime(13) ? "Yes" : "No") << endl;
cout << "n2 is " << n2.getValue() << " it is " << (MyInteger::isEven(20) ? "Even" : "Odd") << endl;
cout << "n2 is " << n2.getValue() << " it is " << (MyInteger::isOdd(20) ? "Even" : "Odd") << endl;
cout << "n2 is " << n2.getValue() << " it is " << (MyInteger::isPrime(20) ? "Yes" : "No") << endl;
}
#include <iostream>
#include<string>
class MyInteger
{
// int data field name value
private:
int value;
public:
//myinteger object for the specified int value
MyInteger(int value);
int getValue()const;
/*constant functions return true if the value is Prime. Static functions isPrime(int)
and isPrime(constmyinteger&) return true if the specified value is prime.*/
static bool isPrime(int prime);
bool isPrime()const;
static bool isPrime(const MyInteger& p);
/*constant functions return true if the value isEven,static functions isEven(int)
and isEven(constmyinteger&) return true if the specified value is prime.*/
static bool isEven(int even);
bool isEven()const;
static bool isEven(const MyInteger& e);
/*constant functions return true if the value is Odd. static functions isOdd(int)
and isOdd(constmyinteger&) return true if the specified value is prime. */
static bool isOdd(int odd);
bool isOdd()const;
static bool isOdd(const MyInteger& o);
/*constant functions equals(int) and equals(const myintegers&)
that return true if the value in the object is equal to the specified value.*/
bool equals(const MyInteger& q)const;
bool equals(int num)const;
////static function parseInt(const string&)that converts string to an int value
//static int parseInt(const string& s)
//{
// s.c_str();
// return 1;
//}
#ifndef MYINTEGER_H
#define MYINTEGER_H
using namespace std;
#include"MyInteger.h"
//myinteger object for the specified int value
MyInteger::MyInteger(int value)
{
this->value = value;
}
//get function that returns the int value
int MyInteger::getValue()const
{
return value;
}
/*constant functions is Prime. Static functions isPrime(int)and isPrime(constmyinteger&) */
bool MyInteger::isPrime(int prime)//static
{
if (prime == 1 || prime == 2)
return true;
for (int i = 2; i <= prime / 2; i++)
{
if (prime % i == 0)
return false;
}
a string is made up of characters.
so if the user types in 1234 it is made up of 4 characters: '1', '2', '3', and '4'
you need to turn this into an integer.
one way to do that without using c++ or c built-in tools is to iterate over each character like this:
int result = 0;
for(i = 0; i < mystring.size(); i++)
{
result *= 10;
result += mystring[i] - '0';
}
that is the 'meat' of the problem, but you need to add code to handle + or - leading symbols (+ is unusual but legal) and reject strings that have non numeric text in them, and you may or may not want to discard leading zeros (application / requirements determine this) or reject strings with leading zeros. Data validation can get rather complicated in a hurry; this is one place requirements are really critical.
Personally I would take a hint like 'use the c-string from string' to mean you can use ATOI (built in text to integer from C). But I do not know what your teacher was thinking there, that is just MY take on it because I am passing lazy.