find function in string not working

I've been working on this code for days and just can't make it work. Everything else in here but the getTexas function is working. What am I doing wrong!?!?!? I've tried like 10 different ways of running this function. The function getTexas is supposed to identify all of the names that are in TX and then write them out. Please help!!! I'm going crazy with this function!! The errors are:
Compilation error time: 0 memory: 3436 signal:0
prog.cpp: In function ‘void getTexas(std::string*, std::string*, std::string*, int)’:
prog.cpp:56:27: warning: writing into constant object (argument 2) [-Wformat=]
scanf("%s", tx[i].c_str());
^
prog.cpp:57:18: error: expected primary-expression before ‘]’ token
printf("%s", tx[].c_str());
^
prog.cpp: In function ‘int main()’:
prog.cpp:69:27: error: ‘tx’ was not declared in this scope
getTexas (name, state, tx);
^



#include <string> // for string class
#include <iostream> // for cin and cout
using namespace std;

int initArrays (int m, string name [], double age [], string state []) {
int count = 0;
cout << "Enter the name, age and state of each family member on a single line in the following format: Name Age State (two letter abbreviation). When finished type * to end: ";
while (count < m) {
cin >> name[count];
if (name[count].compare ("*") == 0) break;
cin >> age[count] >> state[count];
// state[count] = getTexas (state[count]);
count ++;
}
return count;
}

void printAll (string name [], double age [], string state [], int m) {
printf ("\n%-20s%10s%8s\n",
"Family Member Name", "Age", "State");
for (int i = 0; i < m; i++)
printf ("%-20s%11.2f%5s\n", name[i].c_str(), age[i], state[i].c_str());
}

double getAverage (double age [], int m) {
if (m <= 0) return 0; // make sure we don't divide by 0
double sum = 0;
for (int i = 0; i < m; i++)
sum = sum + age[i];
return sum/m;
} // end getAverage

void getTexas (string name [], string state [], string tx [], int i){
if (state [i] == "tx")
scanf("%s", tx[i].c_str());
printf("%s", tx[].c_str());
}

int main () {
const int max = 50; // needs to be a constant to use in array declarations
int used; // the number of student entries actually active
string name [max];
double age [max];
string state [max];

used = initArrays (max, name, age, state);
printAll (name, age, state, used);
getTexas (name, state, tx);
cout << "The family's average age is: " << getAverage (age, used) << endl;
cout << "Bye" << endl;
// system ("Pause"); // Visual C++
} // end main
Last edited on
Few things:

You used scanf and printf in the same program that you used cin. Why not just use cout? This is C++, after all. That will, at the very least, fix the more obscure errors.

The other error, then, is in your functioncall to getTexas(name,state,tx). The issue is that, in main(), there is no array called tx.
I added the string tx [] but still not working. I was using cin and cout at first, but kept getting even more errors about syntax and formatting, so tried the scanf and printf instead because I know I was able to make those work earlier in the code. I know it looks like a hot mess, but that's from working on it for over a week on this single function... it's gone through quite a few changes... thanks for the suggestions, though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <string> // for string class
#include <iostream> // for cin and cout
using namespace std;

int initArrays ( int m, string name [], double age [], string state [] )
{
    int count = 0;
    cout << "Enter the name, age and state of each family member on a single line in the following format: Name Age State (two letter abbreviation). When finished type * to end: ";
    while ( count < m )
        {
            cin >> name[count];

            //if ( name[count].compare ( "*" ) == 0 ) break; // ************
            if( name[count] == "*" ) break ;

            cin >> age[count] >> state[count];
// state[count] = getTexas (state[count]);
            count ++;
        }
    return count;
}

void printAll ( string name [], double age [], string state [], int m )
{
    printf ( "\n%-20s%10s%8s\n",
             "Family Member Name", "Age", "State" );
    for ( int i = 0; i < m; i++ )
        printf ( "%-20s%11.2f%5s\n", name[i].c_str(), age[i], state[i].c_str() );
}

double getAverage ( double age [], int m )
{
    if ( m <= 0 ) return 0; // make sure we don't divide by 0
    double sum = 0;
    for ( int i = 0; i < m; i++ )
        sum = sum + age[i];
    return sum / m;
} // end getAverage


//********************************
//void getTexas ( string name [], string state [], string tx [], int i )
// The function getTexas is supposed to identify all of the names that are in TX and then write them out.
void printTexas ( const string name [], const string state [], int m )
{
    puts( "names of residents of Texas\n-------------------- " ) ;

    for( int i = 0 ; i < m ; ++i )
        if ( state [i] == "tx" || state [i] == "TX" ) puts( name[i].c_str() ) ;
    puts( "----------------------------" ) ;
}
//********************************

int main ()
{
    const int max = 50; // needs to be a constant to use in array declarations
    int used; // the number of student entries actually active
    string name [max];
    double age [max];
    string state [max];

    used = initArrays ( max, name, age, state );
    printAll ( name, age, state, used );

    // getTexas ( name, state, tx );
    printTexas( name, state, used ) ;

    cout << "The family's average age is: " << getAverage ( age, used ) << endl;
    cout << "Bye" << endl;
// system ("Pause"); // Visual C++
} // end main  


Get familiar with the C++ input/output library. Start by looking at <iomanip>.
https://stdcxx.apache.org/doc/stdlibug/33.html

Don't end up as one of those programmers who remain eternally ignorant about how flexible and powerful its architecture really is; who have never managed to shake off their irrational phobia of this elegant library.
Last edited on
You are completely and totally awesome!!! Thank you sooooo much!! Seriously! I start the actual coding class next week, this was for an algorithm class which was a pre-req! I'm trying to figure out as much as I can, but hopefully I'll start actually learning it soon!! :D
Topic archived. No new replies allowed.