How can I get my code to work with string instead of const char?

I've gotten my code to do what the assignment asks except without the proper function that the professor wants. I used a constant char to compare to strings as numbers, but the professor wants them to be declared in the function as a string. How could I do that with my current code?
the proper function signature is:
int compareVersions(string ver1, string ver2)
my signature however is:
int compareVersions(const char *version1, const char *version2)

How can I make it into a function that works with string?

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
/**
 * Assignment#6.1
 * DUE: 3/13/16 
 * NAME: <Joshua Cisneros>
 * Purpose: Write the following function that compares the two version strings, ver1 and ver2, 
 * of a software product to determine which product version is the latest:
 * int compareVersions(string ver1, string ver2)
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
/*
 * return 1 if v1 > v2
 * return 0 if v1 = v2
 * return -1 if v1 < v2
 */
using namespace std;

int compareVersions(const char *version1, const char *version2)
{
    int i;
    int oct_version1[3], oct_version2[3];
    sscanf(version1, "%d.%d.%d", &oct_version1[0], &oct_version1[1], &oct_version1[2]);
    sscanf(version2, "%d.%d.%d", &oct_version2[0], &oct_version2[1], &oct_version2[2]);

    for (i = 0; i < 4; i++) {
        if (oct_version1[i] > oct_version2[i])
            return 1;
        else if (oct_version1[i] < oct_version2[i])
            return -1;
    }

    return 0;
}

int main()
{
   std::cout << ("%d\n", compareVersions("0.1.2", "0.2.3")) << endl;
   std::cout << ("%d\n", compareVersions("3.2.0", "3.2.0")) << endl;
   std::cout << ("%d\n", compareVersions("5.2.0", "3.2.0")) << endl;
}
A simple way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int compareVersions (const string& version1, const string& version2)
{
  int i;
  int oct_version1[3], oct_version2[3];
  sscanf (version1.c_str(), "%d.%d.%d", &oct_version1[0], &oct_version1[1], &oct_version1[2]);
  sscanf (version2.c_str(), "%d.%d.%d", &oct_version2[0], &oct_version2[1], &oct_version2[2]);

  for (i = 0; i < 4; i++)
  {
    if (oct_version1[i] > oct_version2[i])
      return 1;
    else if (oct_version1[i] < oct_version2[i])
      return -1;
  }

  return 0;
}


It's a bit cheeky, so I am not sure if your professor accepts it.

Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int compareVersions (const string& version1, const string& version2)
{
  int i;
  int oct_version1[3], oct_version2[3];

  oct_version1[0] = version1[0]; 
  oct_version1[1] = version1[2];
  oct_version1[2] = version1[4];

  oct_version2[0] = version2[0];
  oct_version2[1] = version2[2];
  oct_version2[2] = version2[4];

  for (i = 0; i < 4; i++)
  {
    if (oct_version1[i] > oct_version2[i])
      return 1;
    else if (oct_version1[i] < oct_version2[i])
      return -1;
  }

  return 0;
}
Well, the easiest way to do this with what you have is use std::string's c_str() function.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int compareVersions(const string & version1, const string & version2)
{
    int i;
    int oct_version1[3], oct_version2[3];
    sscanf(version1.c_str(), "%d.%d.%d", &oct_version1[0], &oct_version1[1], &oct_version1[2]);
    sscanf(version2.c_str(), "%d.%d.%d", &oct_version2[0], &oct_version2[1], &oct_version2[2]);

    for (i = 0; i < 3; i++) {
        if (oct_version1[i] > oct_version2[i])
            return 1;
        else if (oct_version1[i] < oct_version2[i])
            return -1;
    }

    return 0;
}


Line 10: You have the wrong header to use std:;string. The correct header is #include <string>

Lines 23-24: If this is a C++ class, sscanf is a poor choice for extracting the subfields. You should be using a C++ idiom. It's poor style to mix C and C++.

Line 26: You're executing your loop one too many times. You're going to execute the loop with i=0,1,2,3, which is 4 times, but oct_version[] has only 3 elements. oct_version[3] will be an out of bounds reference.
Topic archived. No new replies allowed.