0
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
    ifstream input("input.txt");
    string names[20];
    string temp;
    int vote=0;
    int votes[10];

    for(int i =0; i < 20; i++)
    {
        input>>temp;
        if (strcmp(temp,"9")>0)
        {
            names[i]=temp;
        }
        else
        {
            break;
        }
    }
}

So I want to read in a file and store every name(ie. James Tylon) into a string array and when it detect an int, it will stop. However when I use this approach, error comes out.

error C2664: 'strcmp' : cannot convert parameter 1 from 
 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' 
 to 'const char *'
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
GzAndy
  • 141
  • 3
  • 10
  • 1
    _`if (strcmp(temp,"9")>0)`_ sure?? What's the point of `std::string` and not actually using it? Also have a look [here](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) please. – πάντα ῥεῖ Aug 29 '15 at 18:33
  • Aren't you suppose to use the [string::compare](http://www.cplusplus.com/reference/string/string/compare/) method instead of strcmp ? – dvhh Aug 29 '15 at 18:37
  • What do you want that `strcmp` call to do? Is "123a" a string or integer? What about "a23", or " 12z56"? – Alan Stokes Aug 29 '15 at 19:34
  • Note that you are actually storing every **word** by using `>>`, so name like `James Tylon` will be separated. – Thomas Lee Aug 29 '15 at 19:37

3 Answers3

2

Following code will check for a number:

if (temp.find_first_not_of ("0123456789") == std::string::npos ) {
     std::cout << "is an integer!" << std::endl;
}
adnan kamili
  • 8,967
  • 7
  • 65
  • 125
0

If you are using strcmp then you should supply it two char * parameters and not std::string. That causes the compilation error you mentioned. You can fix it like that: strcmp(temp.c_str(), "9").

Also according to your example I see that you expect an integer at the first char, that means that anything that starts with a letter and has a digit inside of it is a legal value which you classify as string. In this case you could test it by:

if (static_cast<unsigned>(temp[0] - '0') <= 9) 
{
    /* Digit detected */
}
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
0

You can use regex to find whether the value is an int or not.

Here is a simple example:

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main() {
    string a[2] = {"this string contains integer 1423 and character", "100"} ;
    regex integer("(\\+|-)?[[:digit:]]+");

    for (int i=0; i < sizeof(a)/sizeof(string); i++) {
        if (regex_match(a[i], integer)) {
            cout << a[i] << ": contains integer only\n";
        } else {
            cout << a[i] << ": doesn't contain integer only\n";
        }
    }

    return 0;
}

Output:

this string contains integer 1423 and character: doesn't contain integer

100: contains integer

sam
  • 2,033
  • 2
  • 10
  • 13