How to find positions of repeated characters in a string? - c++

How can I find the positions of repeated characters in a string? For example, the inputted string is "11-28-1995". My goal is to assign each of those values between the "-", a variable, so I would have month = 11, day - 28, and year = 1995. This is what I have got so far, although I am not sure how to iterate after the month.
#include <iostream>
using namespace std;
int main() {
string input;
cout << "What is the date?" << endl;
cin >> input;
int pos = input.find("-");
int month = stoi(input.substr(0, pos));
cout << month << endl;
cout << day << endl;
return 0;
}

Maybe you are looking for something like this:
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
int main() {
string input;
cout << "What is the date?" << endl;
cin >> input;
unsigned int day = 0;
unsigned int month = 0;
unsigned int year = 0;
stringstream ostr(input.c_str());
char c1, c2 = 0;
ostr >> day >> c1 >> month >> c2 >> year;
assert((c1 == c2) && (c2 == '/'));
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
return 0;
}
For a more general token extractor, you may be interested in the use of getline:
use getline and while loop to split a string
Hope it helps ;)

Related

How to create multiple file in fstream with loop [duplicate]

This question already has answers here:
How to concatenate a std::string and an int
(25 answers)
Closed 2 years ago.
I am new in coding and in this community I want to save multiple student info in multiple file in ".txt" file like "student1.txt", "student2.txt", "student3.txt" etc. See my code then I hope you will understand my problem.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
for(int i = 1; i < 30; i++) {
string name, roll, gpa;
cout << "Enter student info";
cout << "name :";
cin >> name;
cout << "\nEnter Roll :";
cin >> roll;
cout << "\nEnter gpa :";
cin >> gpa;
ofstream file;
/* Problem part :I know this part of code will never work */
file.open("Student <<i<<".txt");
/* what should I do */
file << name << endl << roll << endl << gpa;
file.close();
}
}
Here is what I think you need: std::to_string and operator+ (string)
Check out the answers on this thread. There they have shown numerous methods to do this.
Code:
#include <fstream>
#include <iostream>
#include <string>
int main() {
for (int i = 1; i < 30; i++) {
std::string name, roll, gpa;
std::cout << "Enter student info : " << std::endl;
std::cout << "Name : ";
std::cin >> name;
std::cout << "Enter roll number : ";
std::cin >> roll;
std::cout << "Enter GPA : ";
std::cin >> gpa;
std::ofstream file("Student" + std::to_string(i) + ".txt");
file << name << std::endl
<< roll << std::endl
<< gpa << std::endl;
}
}

No match for ‘operator<<’ in std

I just started learning C++, and this test seemed like a good idea so i tried doing it, doesn't seem to work, and it really doesn't make sense why (to me).
#include <iostream>
using namespace std;
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.32543; // Floating point number (with decimals)
char myLetter = 'H'; // Character
string myText = "test text: test"; // String (text)
bool myBoolean = true; // Boolean (true or false)
int main() {
cout << myNum << endl;
cin >> myNum >> endl;
cout << myFloatNum << endl;
cin >> myFloatNum >> endl;
cout << myLetter << endl;
cin >> myLetter >> endl;
cout << myText << endl;
cin >> myText >> endl;
cout << myBoolean << endl;
cin >> myBoolean >> endl;
return 0;
}
You forgot to include <string>, string isn't a basic C++ datatype; use #include <string> after iostream, without the spaces after the greater than and less than signs.
It does not make sense to cin something into endl. cin is a stream to get data from, but the endl is a thing to end the line, as #arsdever commented.
Simply remove it, and your code will compile:
#include <iostream>
#include <string> // You forgot to include that header, for using std::string
using namespace std;
int myNum = 5;
double myFloatNum = 5.32543;
char myLetter = 'H';
string myText = "test text: test";
bool myBoolean = true;
int main() {
cout << myNum << endl;
cin >> myNum;
cout << myFloatNum << endl;
cin >> myFloatNum;
cout << myLetter << endl;
cin >> myLetter;
cout << myText << endl;
cin >> myText;
cout << myBoolean << endl;
cin >> myBoolean;
return 0;
}
Although, you may want to first read the user's input, and then print it. Now, you print the predefined by you value of the variable (and then print an end of line), and then read the input from the user for that specific variable.

Xcode won't recognize/read file properly

I built a program in c++ to calculate the amount of gas money I owe to my dad for my summer job at Domino's. What the program does is read two lines from a file in a do while loop, one for the starting mileage that day and one for the ending mileage of that day. However I just can't get the program to read the file properly. I'm positive I opened it correctly and put it in the right spot (same folder as the .cpp file) but it just won't read it properly. Any ideas?
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("sample.txt");
int days; // number of days Alisha worked
int count = 1;
int totalMileage = 0;
int addMileage;
int startMileage;
int endMileage;
double avgMileage;
double moneyOwed;
double realOwed;
const double PER_MILE = 0.08125;
cout << endl;
cout << "How many days did Alisha work?" << endl;
cin >> days;
days++;
do
{
inputFile >> startMileage;
cout << "The starting mileage for day " << count << " is " << startMileage << endl;
inputFile >> endMileage;
cout << "The ending mileage for day " << count << " is " << endMileage << endl;
addMileage = endMileage - startMileage;
totalMileage = totalMileage + addMileage;
count++;
cout << endl;
}
while (count != days);

Accepting input in relation to an array of structures

I am currently practicing C++ and I am doing this question from the textbook C++ Primer Plus, and I am stuck at this very last step. Basically I am to make an array out of a structure that contains info a car and the issue that I am having trouble with is recording the user's input.
My code:
#include <iostream>
#include <string>
struct car{
std::string make;
int year;
};
int main()
{
std::cout << "How many cars do you wish to catalog? ";
int lim;
std::cin >> lim;
car* info = new car[lim];
for(int i = 0; i<lim; i++)
{
std::cout << "Please enter the make: ";
getline(std::cin, info[i].make); // problem here..
std::cout << "Please enter the year made: ";
std::cin >> info[i].year; // problem here as well :(
}
std::cout << "here is your collection:\n";
while(int i = 0 < lim)
{
std::cout << info[i].make << " " << info[i].year << std::endl;
//std::cout << "here is your collection:\n"
i++;
}
return 0;
}
Can someone help explain why it isn't working?
Specifically, my issue is that it is not getting my input correctly, and my exe file seems to skip the input of the "make" question and jumps to the year..Then it crashes into oblivious..possibly a segmentation fault.
After you read numbers using
std::cin >> lim;
and
std::cin >> info[i].year;
a newline character is left on the stream, which is picked up by getline as valid input.
You need to add code to ignore the rest of the line.
std::cin >> lim;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
and
std::cin >> info[i].year;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
See documentation on istream::ignore.
Also, change
while(int i = 0 < lim)
{
std::cout << info[i].make << " " << info[i].year << std::endl;
//std::cout << "here is your collection:\n"
i++;
}
to
for(int i = 0; i < lim; ++i)
{
std::cout << info[i].make << " " << info[i].year << std::endl;
//std::cout << "here is your collection:\n"
}
#include <iostream>
#include <string>
struct car{
std::string make;
int year = 0;
};
int main()
{
int i = 0; //increment value
std::cout << "How many cars do you wish to catalog? ";
int lim;
std::cin >> lim;
car* info = new car[lim];
for(i; i < lim; i++)
{
std::cout << "Please enter the make: ";
std::cin >> info[i].make; // change to cin, just like the one for year
std::cout << "Please enter the year made: ";
std::cin >> info[i].year; // this was fine
}
std::cout << "here is your collection:\n";
i = 0; //resets the increment value
while(i < lim)
{
std::cout << info[i].make << " " << info[i].year << std::endl;
//std::cout << "here is your collection:\n"
i++;
}
return 0;
}
combining cin and getline is ... funky. cin never read the newline character from the first string, so your first getline call is going to pretty much just return a blank string. What I normally do when I have an issue like this is perform a throwaway getline() call after my cin.
combining getline and cin is not normally very friendly. Maybe you should switch to all getlines and do a little bit of string manipulation?

Using functions in C++ (void, double, int)

I am slowly trying to learn c++ on my own and got stuck using functions. I did find a way to get past the initial problem but I have no idea why I couldn't do it the way I first intended to. Here's the working program.
// ex 6, ch 2
#include <iostream>
using namespace std;
void time(int, int);
int main()
{
int h, m;
cout << "Enter the number of hours: ";
cin >> h;
cout << endl;
cout << "Enter the number of minutes: ";
cin >> m;
cout << endl;
time(h, m);
cin.get();
cin.get();
return 0;
}
void time(int hr, int mn)
{
cout << "The time is " << hr << ":" << mn;
}
And here is how I would like to do it.
// ex 6, ch 2
#include <iostream>
using namespace std;
void time(int, int);
int main()
{
int h, m;
cout << "Enter the number of hours: ";
cin >> h;
cout << endl;
cout << "Enter the number of minutes: ";
cin >> m;
cout << endl;
cout << "The time is " << time(h, m);
cin.get();
cin.get();
return 0;
}
void time(int hr, int mn)
{
cout << hr << ":" << mn;
}
In my head both of them would return the same thing but my compiler thinks otherwise (and I would like to know why).
Edit: It seems to work like this for some odd reason.
cout << "The time is ";
time(h, m);
If nothing more, it just made me more confused.
cout << "The time is " << time(h, m);
time does not return anything, but to send something to cout in this case would require it to return a value (probably a string in this case) vs having the time function call cout directly.
You need to edit your time-function to return a string. I'm using stringstream to convert int to string.
#include <sstream>
...
string time(int, int);
...
string time(int hr, int mn)
{
stringstream sstm;
sstm << hr << ":" << mn;
string result = sstm.str();
return result;
}
Now you can use it directly, like:
cout << "The time is " << time(h, m);