Question

In: Computer Science

This is in C++ Write a program that reads a string consisting of a positive integer...

This is in C++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, The program must use a stack to convert the decimal number to the numeric format. I keep getting an error around line 31 stating that there is an undefined function stObj.push(*it - 48); -- any help to fix my error would be helpful, thank you in advance.

Here is my code:

#include <iostream>
#include <string>
#include <stack>
#include <math.h>
using namespace std;

//main method
int main()
{
   //Declare local variables
   string input;
   double number = 0;
   int index = 0;

   //declare the stack of type integer
   stack<int> stObj;

   //prompt and read the input number
   //of type string
   cout << "Enter a decimal number:";
   cin >> input;

   //declare iterator
   string::iterator iterObj = input.begin();

   //push the number into the stack
   //increment the index of the stack
   while (*iterObj != '.' && iterObj != input.end())
   {
       //push into the stack
       stObj.push(*it - 48);
       iterObj++;
       index = index + 1;
   }

   //compute the integral part of the number
   for (int i = 0; i < index; i++)
   {
       number = number + (stObj.top()*pow(10, i));
       stObj.pop();
   }

   //consider the fractional part
   index = -1;
   if (iterObj != input.end())
   {
       //increment the interator for the memory address
       iterObj++;
   }
   //compute the fractional part to number
   while (iterObj != input.end())
   {
       number = number + ((*iterObj - 48)*pow(10, index));
       index = index - 1;
       iterObj++;
   }
   //Display the decimal number
   cout << "The numeric format: " << number << endl;
}

Solutions

Expert Solution

Correct C++ Code:

#include <iostream>
#include <string>
#include <stack>
#include <math.h>
using namespace std;

//main method
int main()
{
//Declare local variables
string input;
double number = 0;
int index = 0;

//declare the stack of type integer
stack<int> stObj;

//prompt and read the input number
//of type string
cout << "Enter a decimal number:";
cin >> input;

//declare iterator
string::iterator iterObj = input.begin();

//push the number into the stack
//increment the index of the stack
while (*iterObj != '.' && iterObj != input.end())
{
//push into the stack
stObj.push(*iterObj - 48);
iterObj++;
index = index + 1;
}

//compute the integral part of the number
for (int i = 0; i < index; i++)
{
number = number + (stObj.top()*pow(10, i));
stObj.pop();
}

//consider the fractional part
index = -1;
if (iterObj != input.end())
{
//increment the interator for the memory address
iterObj++;
}
//compute the fractional part to number
while (iterObj != input.end())
{
number = number + ((*iterObj - 48)*pow(10, index));
index = index - 1;
iterObj++;
}
//Display the decimal number
cout << "The numeric format: " << number << endl;
}

Bold change has to be made in your code.

*it is not defined.

It should be changed to *iterObj.

Output:


Related Solutions

In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
Write a program in C that reads prompts the user for a positive integer and then...
Write a program in C that reads prompts the user for a positive integer and then prints out that number in base 16, base 8 and base 2.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
(Please write in C++) Write a program that reads in a line consisting of a student’s...
(Please write in C++) Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ]...
Write a C program that reads an integer value. Assume it is the number of a...
Write a C program that reads an integer value. Assume it is the number of a month of the year; print out the name of that month (Hint: months need to be captured in an array).
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
who to write a program c++ that reads in an integer between 0 and 1000 and...
who to write a program c++ that reads in an integer between 0 and 1000 and adds all the digits in the integer?
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT