Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,557 people online right now. Registration is fast and FREE... Join Now!




Problem Compiling: Array Function

 
Reply to this topicStart new topic

Problem Compiling: Array Function

aznballerlee
4 Nov, 2006 - 04:52 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I want to know why my following code (or parts of my tests don't compile right)

CODE


#include <iostream>
#include <string>
#include <cassert>

using namespace std;


int tally(const string a[], int n, string target);
// Return the number of strings in the array that are equal to target.

int tally(const string a[], int n, string target)
{
    int result;

    if (n < 0)
    {
        result = -1;
        return result;
    }
    
    else if (n==0)
    {
        result = 0;    
        return result;
    }

    else
        int result = 0;
        for (int i=0; i<n; i++)
        {
            if (target == a[i])
                result += 1;
            else
                result;
        }
            return result;


}
int findFirst(const string a[], int n, string target);

int findFirst(const string a[], int n, string target)

// Return the index of the first string in the array that is equal to target.
// Return -1 if there is no such string.

{
    int result = -1;

    if (n <= 0)
        return result;
    else
        for (int i=0; i<n; i++)
        {
            if (a[i] == target)
                result = i;
                break;
                      
        }
            return result;
}
int main()
{
    string a[6] = { "lois", "peter", "", "meg", "", "meg" };
     assert(tally(a,6,"meg") == 2);
     //  assert(tally(a,6,"") == 2);
     // assert(tally(a,6,"chris") == 0);
    assert(tally(a,0,"lois") == 0);
    
    
    // assert(findFirst(a,6,"meg") == 3);
    // assert(findFirst(a,3,"meg") == -1);

    cout << "All tests succeeded" << endl;
}




CODE

assert(tally(a,6,"meg") == 2)
passes the test,
but why don't the following?

CODE

assert(tally(a,6,"chris") == 0;
assert(tally(a,6,"") == 2);


This post has been edited by aznballerlee: 4 Nov, 2006 - 04:52 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Problem Compiling: Array Function
4 Nov, 2006 - 08:40 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 4 Nov, 2006 - 05:52 PM) *

I want to know why my following code (or parts of my tests don't compile right)


I managed to compile it in a professional grade compiler, but I believe that you may need assert.h to run properly

CODE

<stripped for simplicity>

// Return the number of strings in the array that are equal to target.
int tally(const string a[], int n, string target)
{
    int result = 0; // << C/C++ value is not preset - programmer must fix

    if (n < 0)
    {
        result = -1;
        //return result; // << code to reduce these
    }
    
    else if (n==0)
    {
        result = 0;    
        //return result;
    }

    else
        //int result = 0; << Redeclared - remove
        for (int i=0; i<n; i++)
        {
            if (target == a[i])
                result += 1; // << result++ is more readable
            else
                result;
        }
            return result; //<< this is not the same as the one redeclared..
                                            // {} is called scoping - change scope - change reference


}
int findFirst(const string a[], int n, string target);

int findFirst(const string a[], int n, string target)

int main()
{
    string a[6] = { "lois", "peter", "", "meg", "", "meg" };

     assert(tally(a,6,"meg") == 2);

    cout << "All tests succeeded" << endl;
}



QUOTE

assert(tally(a,6,"meg") == 2) passes the test,
but why don't the following?

CODE

assert(tally(a,6,"chris") == 0;
assert(tally(a,6,"") == 2);


assert(tally(a,6,"chris") == 0; is missing the closing ), which will definitely stop compiling.

A good habit to get into is to type the close and open '', "", (), [] or {}, then left-arrow

eg
int main(){}

CODE
int main(){
  // start to type inside the brackets,
  cout << "Typed the way I suggested";

}


This post has been edited by gregoryH: 5 Nov, 2006 - 02:40 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:50PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month