Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Code and Syntax Check


Please check my codes, all return errors.  Please advise.  Del


// prob1.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main(void) {

      int count;

      for(int n = 0; n <= count; n++) {
            cout << " Enter a non-negative integer (-1 to stop) " << endl;

      cin >> n;

      if(n < 0)
            break;

      cout << " # of digits = " << numDigits(n) << endl;

      }
      return 0;
      system("pause");
}

int numDigits(unsigned n) {

      int count = 0;

      if(n < 10) return 1;
      else {
            return 1 + numDigits = (n/10);

      }

}

output:

prob1.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob1.cpp(22) : error C2064: term does not evaluate to a function taking 1 arguments
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob1.cpp(35) : error C2297: '+' : illegal, right operand has type 'int (__cdecl *)(unsigned int)'
Build log was saved at "file://c:\Documents and Settings\Enrique\My Documents\Visual Studio 2005\Projects\Lab 6\Lab 6\Debug\BuildLog.htm"
Lab 6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

************************


// prob2.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void printReverse(unsigned);

int main(void) {

      int count, n, numDigits;

      for(int n = 0; n <= count; n++) {
            cout << " Enter a non-negative integer (-1 to stop) " << endl;

      cin >> n;

      if(n < 0)
            break;

      cout << " # of digits = " << numDigits(n) << endl;

      }
      return 0;
      system("pause");

}

void printReverse(unsigned n) {

      int print_Reverse;
      if(n < 10)
            cout << endl;
      else {
            cout << (n % 10);
      print_Reverse(n/10);

      }
      return;
}

output:

prob2.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob2.cpp(24) : error C2064: term does not evaluate to a function taking 1 arguments
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob2.cpp(39) : error C2064: term does not evaluate to a function taking 1 arguments
Build log was saved at "file://c:\Documents and Settings\Enrique\My Documents\Visual Studio 2005\Projects\Lab 6\Lab 6\Debug\BuildLog.htm"
Lab 6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

************************


// prob3.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

double power(double, int);

int main(void) {

      int count, n;

      for(int n = 0; n <= count; n++) {
            cout << " Enter a non-negative integer (-1 to stop) " << endl;

      cin >> n;

      if(n < 0)
            break;

      cout << " # of digits = " << numDigits(n) << endl;

      }
      return 0;
      system("pause");
}

double power(double x, int a) {

      if(n == 0)
            return 1.0;
      
      if(n > 0)
            return x * power(x, n - 1);

      else {
            return 1/x * power(x, n + 1);

      }

}

prob3.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob3.cpp(24) : error C3861: 'numDigits': identifier not found
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob3.cpp(33) : error C2065: 'n' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Enrique\My Documents\Visual Studio 2005\Projects\Lab 6\Lab 6\Debug\BuildLog.htm"
Lab 6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

*****************************


// prob4.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

typedef int ArrayType;

void reverseArray(ArrayType arr, int, int);

int main(void) {

      int count, n;

      for(int n = 0; n <= count; n++) {
            cout << " Enter a non-negative integer (-1 to stop) " << endl;

      cin >> n;

      if(n < 0)
            break;

      cout << " # of digits = " << numDigits(n) << endl;

      }
      return 0;
      system("pause");
}

void reverseArray(ArrayType arr, int first, int last) {

      if(first < last) {
            ArrayType temp = arr[first];
            arr[first] = arr[last];
            arr[last] = temp;
            reverseArray(arr, first + 1, last - 1);

      }
      return;

}

output:

prob4.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob4.cpp(26) : error C2064: term does not evaluate to a function taking 1 arguments
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob4.cpp(36) : error C2109: subscript requires array or pointer type
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob4.cpp(37) : error C2109: subscript requires array or pointer type
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob4.cpp(37) : error C2109: subscript requires array or pointer type
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\lab 6\prob4.cpp(38) : error C2109: subscript requires array or pointer type
Build log was saved at "file://c:\Documents and Settings\Enrique\My Documents\Visual Studio 2005\Projects\Lab 6\Lab 6\Debug\BuildLog.htm"
Lab 6 - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0
edelossantos
Asked:
edelossantos
  • 2
2 Solutions
 
AlexFMCommented:
prob1.cpp - 3 errors are fixed, see my comments in the text

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int numDigits(unsigned n);   // add this line: forward declaration

int main(void) {

//    int count;           // You don't need this variable
    int n;

    //for(int n = 0; n <= count; n++) {
    while ( true )                        // loop which ends when user enters negative number
    {
        cout << " Enter a non-negative integer (-1 to stop) " << endl;

        cin >> n;

        if(n < 0)
            break;

        cout << " # of digits = " << numDigits(n) << endl;

    }
    return 0;
    system("pause");
}

int numDigits(unsigned n) {

    int count = 0;

    if(n < 10) return 1;
    else {
        return 1 + numDigits(n/10);       // = is removed

    }

}
0
 
edelossantosAuthor Commented:
Is this correct? Del

void reverseArray(ArrayType arr, int first, int last) {

     if(first < last) {
          ArrayType temp = arr[first];
          arr[first] = arr[last];
          arr[last] = temp;
          reverseArray(arr, first + 1, last - 1);

     }
     return;

}
0
 
AlexFMCommented:
Correction: first parameter dhould be ArrayType* (pointer to ArrayType array).

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

typedef int ArrayType;
void reverseArray(ArrayType* arr, int first, int last);

int main(void)
{
    int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    reverseArray(data, 0, sizeof(data)/sizeof(int) - 1);

    for ( int i = 0; i < sizeof(data)/sizeof(int); i++ )
    {
        cout << data[i] << endl;
    }

    return 0;
}

void reverseArray(ArrayType* arr, int first, int last)
{

    if(first < last) {
        ArrayType temp = arr[first];
        arr[first] = arr[last];
        arr[last] = temp;
        reverseArray(arr, first + 1, last - 1);

    }
    return;

}
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now