Roman Numeral Output in C++ is always "-858993460", not sure why? - c++

I am relatively new to C++ and have some experience in Java with classes and functions but now very much, so this program is giving me some issues. Below is the code I have, everything seems right now to me and even though I have set "num" to 0, it always prints out "-858993460".
Here are my header files:
#include <string>
using namespace std;
class romanType
{
public:
void setRoman(string n);
void romanToPositiveInteger();
void printPositiveInteger() const;
romanType();
romanType(string n);
void printNum();
private:
string romanString;
int num;
};
Here is my implementation file:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int value(char num) {
if (num == 'I')
return 1;
if (num == 'V')
return 5;
if (num == 'X')
return 10;
if (num == 'L')
return 50;
if (num == 'C')
return 100;
if (num == 'D')
return 500;
if (num == 'M')
return 1000;
return -1;
}
void romanType::setRoman(string n) {
romanString = n;
}
void romanType::romanToPositiveInteger() {
num = 0;
for (int i = 0; i < romanString.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(romanString[i]);
if (i + 1 < romanString.length())
{
// Getting value of symbol s[i+1]
int s2 = value(romanString[i + 1]);
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
num = num + s1;
}
else
{
num = num + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
num = num + s1;
i++;
}
}
}
void romanType::printPositiveInteger() const {
cout << num << endl;
}
romanType::romanType(string n) {
romanString = n;
}
romanType::romanType() {
}
void romanType::printNum() {
cout << num << endl;
}
And here is my main file:
#include "stdafx.h"
//Main program
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int main()
{
romanType roman;
string romanString;
while (romanString != "EXIT") {
cout << "Enter a roman number: ";
cin >> romanString;
roman.printNum();
roman.setRoman(romanString);
cout << "The equivalent of the Roman numeral "
<< romanString << " is ";
roman.printPositiveInteger();
cout << endl;
cout << endl;
}
//Pause the program
std::cout << "\n\n---------------------------------\n";
system("pause");
//Exit the program
return EXIT_SUCCESS;
}
As I said previously, I am currently held up on the output part, but since I am new and this code is most likely horrid, I am accepting any critique on it. I will be a pretty busy today with work and wont be able to implement any suggestions until the next day, but I will get back to anyone that has a solution as soon as I am able to! Thanks in advance for any help :)

You need to call roman.romanToPositiveInteger() at some point between roman.setRoman(romanString); and roman.printPositiveInteger();

Related

Why is my code stopping prematurely? what have i done wrong?

I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}

How to countdown to 0 C++

I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.
#include <stdio.h>
#include <iostream>
using namespace std;
class Solution {
public:
int num;
int numberOfSteps (int num)
{
while (num != 0)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num;
}
else
{
num = num - 1;
cout << num;
}
}
}
};
int main () {
int num;
Solution myObj;
cin >> num;
cout << myObj.num;
}
You're passing the num to std::cout. You are also not calling numberOfSteps(...) anywhere in your code.
Replacing the line with cout << myObj.numberOfSteps(num); fixes the problem, but a tidier solution would be as follows:
#include <stdio.h>
#include <iostream>
void countDown (int num) {
while (num != 0) {
if (num % 2 == 0) {
num = num / 2;
std::cout << num << std::endl;
} else {
num = num - 1;
std::cout << num << std::endl;
}
}
}
int main () {
int num;
std::cin >> num;
countDown(num);
}
Class is not necessary as there is no state and the function is void since it does not return anything.
I am revisiting this question and have created a simpler solution than my original post:
#include <iostream>
using namespace std;
int num;
int main()
{
cout << "Please enter the number you would like to count down to zero : ";
cin >> num;
while (num > 0)
{
cout << num << endl;
num--;
}
cout << "The number is now zero.";
return 0;
}

s.erase is not working when trying to remove spaces from a string

I am trying to remove the spaces from a string to validate a Palindrome phrase. I have looked up other methods, but my professor literally copy and pasted the remove space for loop in our instructions but I can't get it to work and he says he doesn't want us going to the internet for help. I am trying to remove spaces from a phrase like "too hot to hoot" to validate it. I can get my program to work with single words like "bob", but not phrases.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[100];
cout << "Please enter a word/phrase: ";
cin >> input;
for (int i = 0; i < strlen(input); i++)
{
while (s[i] == ' ')//getting "s" is undefined error
s.erase(i,1);
}
int i = 0;
int j = strlen(input)-1;
bool a = true;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if(a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout<< input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}
Maybe you have not copy the result from temporary variable 's'. So, the modified codes should be:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
char input[100];
cout << "Please enter a word/phrase: ";
fgets(input, 100, stdin);
string s(input); // define a temporary variable 's'
int i = 0;
while (i < s.length())
{
if (s[i] == ' ' || s[i] == '\n')
{
s.erase(i, 1); // erase from variable 's', other then 'input'
continue;
}
i++;
}
// copy result from 's' to 'input'
sprintf(input, "%s", s.c_str());
int j = strlen(input) - 1;
bool a = true;
i = 0;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if (a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout << input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}

How to not print the last comma of a list of integers? [duplicate]

This question already has answers here:
How can I print a list of elements separated by commas?
(34 answers)
Closed 6 years ago.
This is when going through a list of integers in order seperated by commas and I only print one instance of an integer even where there are more than one seperated by commas. (CodeEval challenge https://www.codeeval.com/open_challenges/29/)
My problem is I am trying to do this in linear time without any external storage. And I can't have a comma at the end (e.g. 1,3,4,6,). The solutions I found online all use some list to store the integers and then they do a print.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << endl;
return 0;
}
One of the solution is to use boolean flag:
bool first = true;
for( ... ) {
if( first ) first = false;
else std::cout << ',';
std::cout << data;
}
if (i == str.length() - 1)
{
cout << num;
}
else
{
count << num << ",";
}
Or you could print a backspace at the end of the string processing:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "1,2,2,3,3,3,4,4";
char c;
int num = -1;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
c = str[i];
if ((c - '0') != num) {
num = c - '0';
cout << num << ",";
}
}
}
cout << '\b';
cout << endl;
return 0;
}

How do I convert a string I get from a function from an external file to all upper case [duplicate]

This question already has answers here:
Convert a String In C++ To Upper Case
(31 answers)
Closed 7 years ago.
I am writing a program that can count the number of times a word is found in a external file. The words that are to be searched for are also in an external file but I am able to retrieve those just fine. I realised that it will only update the value of count if the word exactly matches. So for example if I was searching for the word "School" and the word "school" was in the textfile I don't think the value of count would be changed. I also think that count wouldn't be changed if the word to be search for was "SCHOOL" and the word in the textfile was "school". So how do I edit my if statements so that for example the word "school" would match "SCHOOL" AND "School"?
This is my main function:
#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>
using namespace std;
int main() {
int x = 9;
int count = 0;
int count0;
int count1;
int count2;
int count3;
int count4;
int count5;
int count6;
int count7;
int count8;
int count9;
int scount;
const int size = 10;
string word_search[size];
string word;
cout << "Please enter a filename: " << flush;
char filename[30];
cin >> filename;
ReadWords reader(filename);
while (reader.isNextWord()){
count = count + 1;
reader.getNextWord();
}
cout << "There are: " << count << " words in the play" << endl;
cout << "Please enter the name of the file with the search words: " << flush;
char filename1[30];
cin >> filename1;
ReadWords reader1(filename1);
scount = 0;
while (reader1.isNextWord()) {
word_search[scount] = reader1.getNextWord();
++scount;
}
cout << "" << endl;
while (reader.isNextWord()) {
This is where I attempted to convert the input to upper case to see if the word matches the uppercase version of itself but this didn't work. Here I also need to check if the word matches itself if the first letter is capital?
if (reader.getNextWord() == word_search[0] || toupper(reader.getNextWord()) == word_search[0]) {
count0 = count0 + 1;
}
if (reader.getNextWord() == word_search[1]) {
count1 = count1 + 1;
}
if (reader.getNextWord() == word_search[2]) {
count2 = count2 + 1;
}
if (reader.getNextWord() == word_search[3]) {
count3 = count3 + 1;
}
if (reader.getNextWord() == word_search[4]) {
count4 = count4 + 1;
}
if (reader.getNextWord() == word_search[5]) {
count5 = count5 + 1;
}
if (reader.getNextWord() == word_search[6]) {
count6 = count6 + 1;
}
if (reader.getNextWord() == word_search[7]) {
count7 = count7 + 1;
}
if (reader.getNextWord() == word_search[8]) {
count8 = count8 + 1;
}
if (reader.getNextWord() == word_search[9]) {
count9 = count9 + 1;
}
}
cout << "Please enter the name of the file to write to: " << flush;
char filename2[30];
cin >> filename2;
Writer reader2(filename2);
cout << "File has been written too.." << endl;
reader2.writeInt(count);
reader2.writeString("Hello my name is Joshua Ogunnote");
return 0;
}
This is a separate file where some of my functions are declared:
#include "ReadWords.h"
#include <cstring>
#include <iostream>
using namespace std;
void ReadWords::close(){
wordfile.close();
}
ReadWords::ReadWords(const char *filename) {
wordfile.open(filename);
if (!wordfile) {
cout << "could not open " << filename << endl;
exit(1);
}
}
string ReadWords::getNextWord() {
string n;
if(isNextWord()){
wordfile >> n;
int len = n.length();
for(int i = 0; i < len ; i++) {
if (ispunct(n[i]))
{
n.erase(i--, 1);
len = n.length();
}
}
cout << n << endl;
return n;
}
}
bool ReadWords::isNextWord() {
if (wordfile.eof()) {
return false;
}
return true;
}
If you're just using English, a simple tolower() transform will do.
std::string tolower( std::string s )
{
for (char& c : s) c = std::tolower( c );
return s;
}
Now you can compare them:
if (tolower( "Hello" ) == tolower( "HELLO" ))
If you are working with Unicode, you should perform a conversion called case folding on the text, and compare the resulting string data.