- Joined
- Mar 12, 2002
Ok guys so its late, im tired, and I just want this over with.
Ive got this bit of c++ that is crashing.
Well thanks for taking the time to look over this.
Ive got this bit of c++ that is crashing.
Code:
#include <string>
#include <iostream>
using namespace std;//declare namespace
int main(){
//declare variables
bool runit=true;
int opt;
string input,cleaned,upper,reverse;
int start,end;
do{//begin rerun loop
int words=1;
cout << "The program will take an input string, remove all leading and trailing spaces, count the digits and words" << endl;
cout << "Please enter an inital string value, ex. ' Hello world ':";
getline(cin,input);//Get inital input
start=input.find_first_not_of(" ");//find the first real character
end=input.find_last_not_of(" ");//find the last real character
cleaned=(input.substr(start,end));//get the new clean string, from start to end
for (int i=0;i<input.size();i++){
if (isspace(cleaned.at(i))){words++;}//if the char at (i) is a space; increase word count
}
for (int i=0;i<cleaned.size();i++){
upper+=(toupper(cleaned.at(i)));//convert the char at (i) to upper; concantinate to new string
}
for (int i=(cleaned.size()-1);i>=0;i--){//start at the end and move to the beginning.
reverse+=cleaned.at(i);//add the char at (i) to new string
}
//output
cout << "You entered the string: " << input << endl;
cout << "The cleaned string is: " << cleaned << endl;
cout << cleaned << " contains " << cleaned.size() << " Characters" << endl;
cout << "There are " << words << " words in this string" << endl;
cout << "Switched to upper case: " << upper << endl;
cout << "In reverse: " << reverse << endl;
cout << "Would you like to run the program again?\nEnter 1 to continue\nEnter 2 to exit\n";
cin >> opt;
if (opt!=1){runit=false;}//if the user enters anything other than 1 exit
}while(runit);
return 0;
}
Well thanks for taking the time to look over this.