-
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
-
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.
-
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
-
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)
-
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 =
-
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.
}
}
}
}
}
-
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
-
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...