static parseint

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();

bool isPrime(int num);
int main()

{

while (true)

{

system("cls");

cout << "\nMain Menu - Chapter 10\n";

cout << "==================================\n";

cout << " 2: Programming Exercise 10.2\n";

cout << " 4: Programming Exercise 10.4\n";

cout << " 6: Programming Exercise 10.6\n";

cout << " 7: Programming Exercise 10.7\n";

cout << " 10: Programming Exercise 10.10\n";

cout << " 12: Programming Exercise 10.12\n";

cout << " 14: Programming Exercise 10.14\n";

cout << "other: Exit\n";

cout << "==================================\n";

cout << "Enter an exercise: ";

char exercise[2];

cin >> exercise;

cout << endl;



switch (atoi(exercise))

{

case 2: Prog10_2(); break;

//case 4: Prog10_4(); break;

//case 6: Prog10_6(); break;

//case 7: Prog10_7(); break;

case 10: Prog10_10(); break;

//case 12: Prog10_12(); break;

//case 14: Prog10_14(); break;

default: exit(0);

}

cout << endl;

system("pause");

cin.clear();

}

return 0;

}
void Prog10_10()
{
//test this program

MyInteger n1(13);
MyInteger n2(20);

cout << "n1 is "<<n1.getValue ()<< " prime?" <<(n1.isPrime() ? " Yes" :" No") << endl;
cout << "n2 is " << n2.getValue() << " prime?" << (n2.isPrime() ? " Yes" : " No") << endl;
cout << "n1 is " << n1.getValue() << " even? " << (n1.isEven() ? " Yes" : " No" ) << endl;
cout << "n2 is " << n2.getValue() << " even?" << (n2.isEven() ? " Yes" : " No") << endl;
cout << "n1 is " << n1.getValue() << " odd? " << (n1.isOdd() ? " Yes" : " No") << endl;
cout << "n2 is " << n2.getValue() << " odd?" << (n2.isOdd() ? " Yes" : " No") << endl;
cout << "n1 is "<< n1.getValue() <<( n1.equals(5) ? " is equal" : " is not equal") << endl;
cout << "n2 is " << n2.getValue() << (n2.equals(20) ? " is equal" : " is not equal") << endl;

//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;
}

return true;
}
bool MyInteger::isPrime()const//
{
return isPrime(value);
}
bool MyInteger::isPrime(const MyInteger& p)//static
{
return isPrime(p.getValue());

}
/*constant function isEven.Static functions isEven(int)and isEven(constmyinteger&)*/
bool MyInteger::isEven(int even)//static
{
return even % 2 == 0;
}
bool MyInteger::isEven()const
{
return isEven(this->value) ? true : false;

}
bool MyInteger::isEven(const MyInteger& e)//static
{
return isEven(e.getValue());
}
/*constant functions Odd. Static functions isOdd(int) and isOdd(constmyinteger&) */
bool MyInteger::isOdd(int odd)//static
{
return odd % 2 == 1;

}
bool MyInteger::isOdd()const
{
return isOdd(this->value) ? true : false;


}
bool MyInteger::isOdd(const MyInteger& o)//static
{
return isOdd(o.getValue());
}
/*constant functions equals(int) and equals(const myintegers&)*/

bool MyInteger::equals(const MyInteger& q)const
{
return this->value == q.getValue();
}
bool MyInteger::equals(int num)const
{
return num == value;
}
////static function parseInt(const string&)that converts string to an int value
// int MyInteger::parseInt(const string& s)//static
//{
// s.c_str();
// return 1;
//}
#endif


};
You have to look at the characters from left to right and convert numeric characters into a number.
what characters? I really don't understand
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.

Last edited on
Topic archived. No new replies allowed.