Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
StringCalculatorTask/Task.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
133 lines (95 sloc)
3.51 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
int add(string numberString, char delimiter) //Function to add the numbers | |
{ | |
string negNumbers = ""; | |
int lineTotal; //declare a variable for the total for the line | |
vector<int> inputNumbers; //declaring a vector | |
stringstream ss(numberString); //declaring a stringstream | |
//for loop to add integers found to the vector | |
for (int i; ss >> i;) { | |
//check to see if the number is greater than 1000 before adding | |
if (i<1000){ | |
inputNumbers.push_back(i); | |
} | |
//checks if the character is the delimiter | |
if (ss.peek() == delimiter) | |
ss.ignore(); | |
} | |
//try and catch block to check for negatives | |
try{ | |
for (size_t i = 0; i < inputNumbers.size(); i++){ | |
if(inputNumbers[i] < 0){ | |
//add negatives to a string | |
negNumbers += to_string(inputNumbers[i]); | |
negNumbers += ","; | |
} | |
} | |
if(negNumbers != "") | |
{ | |
//return the list of negatives | |
cout << negNumbers << endl; | |
throw 1; | |
} | |
} | |
catch (int e){ | |
cout << "negatives not allowed" << endl; | |
return 0; | |
} | |
//for loop to iterate through the vector and add the numbers to the total | |
for (size_t i = 0; i < inputNumbers.size(); i++){ | |
lineTotal = lineTotal +inputNumbers[i]; | |
} | |
return lineTotal; | |
} | |
int main() | |
{ | |
int Total; //declaring a variable to hold the total | |
string numberString = "1,2,3,4,5,6,7,8\n1,2,3,4"; //number string declared | |
istringstream f(numberString); //istringstream declared | |
string line; //declaring a string to hold a single line | |
bool commaCheck = 0; | |
bool firstLine = 1; | |
char delimiter = ','; | |
//while loop to iterate through each line and send it to be added by the add function | |
while (getline(f, line)) { | |
if(firstLine == 0){ | |
//check to see if there was an invalid comma | |
if(line.back()==delimiter){ | |
cout << "Line must not end with a delimiter" << endl; | |
commaCheck = 1; | |
} | |
//call add function and pass the current line and delimiter | |
else{ | |
Total = Total + add(line, delimiter); | |
} | |
} | |
else{ | |
//set firstLine to false | |
firstLine = 0; | |
//check to see if the first line begins with '//' | |
if(line.rfind("//",0)==0){ | |
//sets delimiter to the character that follows the '//' | |
delimiter = line.at(2); | |
} | |
else{ | |
//check to see if there was an invalid comma | |
if(line.back()==delimiter){ | |
cout << "Line must not end with a delimiter" << endl; | |
commaCheck = 1; | |
} | |
//call add function and pass the current line and delimiter | |
else{ | |
Total = Total + add(line, delimiter); | |
} | |
} | |
} | |
} | |
//check the commaCheck variable before showing final answer | |
if(commaCheck == 0){ | |
cout << Total << endl; //output the final answer | |
} | |
} | |