This creates a string array consisting of 5 strings. Now lets say I let the user input (using cin) what characters these 5 strings will be made of, like below:
word[0][0] accesses the first character of the first string. word[0][1] accesses the second character of the first string. word[2][1] accesses the second character of the third string.
Don't forget to check the length of the string first.
Here is my full program, but for some reason it doesn't work. It works SOMeTIMES, but most of the time it gets the number wrong. Try it out for yourself:
#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
int main()
{
string sequence[5]; // declare array of string
char character;
int cnt = 0, maximum = 0;
cout << "Type in 5 sequences of characters: " << endl << endl;
// lets user input string of characters and stores them in string array
for(int i=0; i<5; i++)
{
cout << "sequence " << i+1 << ": " << flush;
cin >> sequence[i];
cin.ignore(1000, '\n');
cin.clear();
cout << endl;
}
cout << "\n";
cout << "Which character would you like to search for? " << flush;
cin >> character;
// loops through each character of each sequence to find a match.
for(int i=0; i<5; i++)
{
for(int k=0; k<sequence[i].length(); k++)
{
if(sequence[i][k]==character)
cnt++;
}
if(cnt>maximum)
{
maximum=cnt;
cnt=0;
}
}
cout << "\n";
cout << character << " has been used a maximum of " << maximum << " times" << endl;
cin.get();
}
If I enter entire phrases (with spaces between words), it gets the number completely wrong. If I enter single words it gets it right, but only sometimes.
I have no idea what im doing wrong. Help would eb much appreciated.
[For the record, I know the solution is available in the link. Im purposely not looking at it as I want to try and figure this out myself and with some hints from you guys]
Doesn't seem like something that should be mentioned without at least a little bit of elaboration. Can you explain what you mean by this? Why is there no such thing as a 2-Dimensional array?
This only reads a single word at a time. If you want to read the entire line, use:
Good idea. Just tried it, but still it doesn't work.
Computer memory is one dimensional - you only need a single number to represent a memory address. Using math, you can simulate a 2D array such as an image, but it's still 1D in memory. You can also have an array of arrays, but everything is still 1D. Personally, I find it more confusing to think about something as 2D when it isn't in reality - maybe you find it easier.
Please post your current code and I'll try running it.
#include <iostream>
#include <string>
int main()
{
const std::size_t NUM_STRINGS = 5 ;
std::string sequence[NUM_STRINGS] ;
std::cout << "Type in 5 sequences of characters:\n\n" ;
for( std::size_t i = 0 ; i < NUM_STRINGS ; ++i )
{
// by default, std::cout is tied to std::cin.
// Unless we break the tie, std::cout is automatically flushed
// before each input operation on std::cin
std::cout << "sequence " << i+1 << ": " ;
std::getline( std::cin, sequence[i] ) ;
}
std::cout << "\nWhich character would you like to search for? " ;
char character ;
std::cin >> character;
int max_occurrences = 0 ;
// favour range-based loops over classical loops
// http://www.stroustrup.com/C++11FAQ.html#forfor( std::string str : sequence ) // for each string in the array
{
int num_occurrences_in_str = 0 ;
for( char c : str ) // for each character in the string
if( c == character ) ++num_occurrences_in_str ;
if( num_occurrences_in_str > max_occurrences )
max_occurrences = num_occurrences_in_str ;
}
std::cout << "\ncharacter '" << character << "' has been used a maximum of "
<< max_occurrences << " times.\n" ;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string sequence[5]; // declare array of string
char character;
int cnt = 0, maximum = 0;
cout << "Type in 5 sequences of characters: " << endl << endl;
// lets user input string of characters and stores them in string array
for(int i=0; i<5; i++)
{
cout << "sequence " << i+1 << ": " << flush;
getline(cin, sequence[i]);
cout << endl;
}
cout << "\n";
cout << "Which character would you like to search for? " << flush;
cin >> character;
// loops through each character of each sequence to find a match.
for(int i=0; i<5; i++)
{
for(int k=0; k<sequence[i].length(); k++)
{
if(sequence[i][k]==character)
cnt++;
}
if(cnt>maximum)
{
maximum=cnt;
cnt=0;
}
}
cout << "\n";
cout << character << " has been used a maximum of " << maximum << " times" << endl;
cin.get();
}
@JLBorges: Sorry, I dont understand what you are doing with the loops and the link does not help. I'd love to learn it later, but I'd rather not bother with that right now.
I'd like to write this program using normal loops, thanks.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string sequence[5]; // declare array of string
char character;
int cnt = 0, maximum = 0;
cout << "Type in 5 sequences of characters: " << endl << endl;
// lets user input string of characters and stores them in string array
for(int i=0; i<5; i++)
{
cout << "sequence " << i+1 << ": " << flush;
getline(cin, sequence[i]);
cout << endl;
}
cout << "\n";
cout << "Which character would you like to search for? " << flush;
cin >> character;
// loops through each character of each sequence to find a match.
for(int i=0; i<5; i++)
{
for(int k=0; k<=sequence[i].length(); k++)
{
if(sequence[i][k]==character)
cnt++;
}
if(cnt>maximum)
maximum=cnt;
cnt=0;
}
cout << "\n";
cout << character << " has been used a maximum of " << maximum << " times" << endl;
cin.get();
}
you really ought to get into the habit of using sequence[i].at(k)
the difference being that if(sequence[i][k]==character) will not complain if k = 1000000 and if(sequence[i].at(k)==character) will stop your program and you may not understand exceptions now, but they will be very helpful later in debugging your code.