Help with HW

This is a discussion on Help with HW within the C++ Programming forums, part of the General Programming Boards category; Yes I have read the policy about the homework, and no I'm not pretending you to do my HW; I ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Unhappy Help with HW

    Yes I have read the policy about the homework, and no I'm not pretending you to do my HW; I just need some help to find out what's wrong with my code, since it won't compile - or at least it compiles but block itself and gives an error-
    Code:
    #include <string>
    #include <cctype>
    #include <algorithm>
    #include <iostream>
    
    
    
    using namespace std;
    const int ROWS = 5;
    const int COLS = 5;
    int main(int argc, char *argv[])
    {
        string convert(char [ROWS][COLS], string);
        char val[ROWS][COLS] = {'a', 'b', 'c', 'd', 'e',
                                  'f', 'g', 'h', 'i', 'j',
                                  'l', 'm', 'n', 'o', 'p',
                                  'q', 'r', 's', 't', 'u',
                                  'v', 'w', 'x', 'y', 'z'};
        string text;
        int i;
        cout << "please enter a string: ";
        getline(cin, text);
        convert(val, text);
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    string convert(char val[ROWS][COLS], string text) {
    int i, j, k;
    for (k=0; i<text.length(); k++){
            for (i = 0; i < ROWS; ++i) {
                for (j = 0; j < COLS; ++j) {
                    if(val[i][j] = text.at(k) ) {
                         cout << i << "/" << j << " ";
          }
        }
      }
      }
    }
    an example of output should be:
    Code:
    please enter a string: qwe
    4/1 5/2 1/5
    thank you
    Last edited by DontBugMe; 04-10-2006 at 01:45 PM.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    I can't understand why people asking for help don't post the errors they have. It's not that one can't find the problem with the code, it's just that it would make everything sooooooo much easier.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Question

    there's no actual identity of the error - the only thing is:

    AppName: hw.exe AppVer: 0.0.0.0 ModName: hw.exe
    ModVer: 0.0.0.0 Offset: 00008599

    that's it

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    I don't want to just give you the answer, but it would help you out a ton if you studied more about 1: function prototypes, and 2: how to pass a 2-dimensional array to a function.

    here's a hint: where should your function prototype be? (obviously where it is now is wrong)
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well how about fixing some warnings?
    Code:
    $ g++ -W -Wall -ansi -pedantic -O2 foo.cpp
    foo.cpp: In function ‘int main(int, char**)’:
    foo.cpp:18: warning: missing braces around initializer
    foo.cpp:18: warning: missing braces around initializer
    foo.cpp:18: warning: missing braces around initializer
    foo.cpp:18: warning: missing braces around initializer
    foo.cpp:20: warning: unused variable ‘i’
    foo.cpp: At global scope:
    foo.cpp:11: warning: unused parameter ‘argc’
    foo.cpp:11: warning: unused parameter ‘argv’
    foo.cpp: In function ‘std::string convert(char (*)[5], std::string)’:
    foo.cpp:31: warning: comparison between signed and unsigned integer expressions
    foo.cpp:34: warning: suggest parentheses around assignment used as truth value    <- Yes, this is the biggie
    foo.cpp:31: warning: ‘i’ is used uninitialized in this function
    foo.cpp:40: warning: control reaches end of non-void function
    > if(val[i][j] = text.at(k) )
    Try ==, not =
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    As someone else mentioned your function prototype is defined in the wrong location. It should be declared like your CONST variables; that is at the beginning of the file and not inside the main function. Also, your function is set up to return a string, but your actually not returning anything so void might be more appropriate. Also, passing arrays to functions isn't obvious and requires a different syntax.

    As for the rest of your code you are very close. Your looping structure is correct, but with some minor problems that are not actually errors. For instance:
    Code:
    (val[i][j] = text.at(k) )
    Doesn't actually do a comparison, but rather an assignment (A common mistake)
    Code:
    (val[i][j] == text.at(k) )  // Add in extra equal sign
    Anyways here is your code working. Very little was changed.

    Code:
    #include <string>
    #include <cctype>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    const int ROWS = 5;
    const int COLS = 5;
    
     void convert(string);  // Void instead of String and not passing in an array.
    
    int main()  // Removed command line parameter code.
    {
       string text;
    
        cout << "Please enter a string: ";
        cin >> text;  // Just chose an alternative input method.
    
        convert(text);
    
        return 0;  // Return 0.
    }
    
    void convert(string text) 
    {
    	char val[ROWS][COLS] = {'a', 'b', 'c', 'd', 'e',
    												 'f', 'g', 'h', 'i', 'j',
    												 'l', 'm', 'n', 'o', 'p',
    												 'q', 'r', 's', 't', 'u',
    												 'v', 'w', 'x', 'y', 'z'};
    
    	for (int k = 0; k < text.length(); k++)  // Changed i to k in k < text.length();
    	{
    		for (int i = 0; i < ROWS; ++i) 
    		{
    			for (int j = 0; j < COLS; ++j) 
    			{
    				if(val[i][j] == text.at(k) )  // Added extra equal sign.
    				{
    					cout << i + 1 << "/" << j + 1 << " "; // Added 1 before printing since arrays start at zero.
    				}
    			}
    		}
    	}
    }
    Last edited by Kurisu; 04-10-2006 at 05:47 PM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Question

    i was restricted on the various elements to be used, so with some patience I got it to work, thanks anyways to those who tried to help.

    However I'm having an issue on the same program. In the STRING function I must 'return' the final string result.

    It works perfectly with a cout << result << endl; BUT I'm restricted to use return, which ( return result; OR return string(result) won't work, or I should say that I compiles but does not actually display the string - and yes, I did try declaring 'result' as a global variable as well as a local variable.

    Any ideas?

    Thanks

  8. #8
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    if you're returning something from a function, then you have to assign the return value to something. so in main() you need something like this:
    Code:
    stringVar1 = convert(text);
    cout << stringVar1;
    but don't forget to change the return type in your function prototype and definition.
    you can just return a string, but you first have to make the string based on the char array you have...
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 12:00 PM
  2. Replies: 10
    Last Post: 12-05-2008, 11:47 AM
  3. Replies: 6
    Last Post: 05-28-2008, 02:54 AM
  4. ioctl request to get the HW address
    By threahdead in forum Linux Programming
    Replies: 7
    Last Post: 01-03-2008, 12:34 AM
  5. Hw
    By SAMSAM in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-23-2003, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21