Friday, January 8, 2010

C++, help me check my coding! ?

I encounter error on this multiple source program and i cant track down the error..help plz!!





//header.h





#include';video.cpp';





char selection(char n)





//mainVideo.cpp





#include%26lt;iostream%26gt;


#include%26lt;cstdlib%26gt;


using namespace std;





#include';header.h';





main()


{


int pwd;


char n;





cout%26lt;%26lt;';\t .:: Welcome to AJ Video Store System ::.';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';Enter your password: ';;


cin%26gt;%26gt;pwd;





if (pwd = 1234)


{


cout%26lt;%26lt;';Successful log in!';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';\t .:::: Video Store System Menus ::::. ';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;'; A) Add a new video record ';%26lt;%26lt;endl;


cout%26lt;%26lt;'; B) Exit ';%26lt;%26lt;endl;





cout%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';Please enter your menu: ';;


cin%26gt;%26gt;n;





system(';CLS';);





n = selection();


}





else


{ cout%26lt;%26lt;'; Incorrect Password!';%26lt;%26lt;endl;


}





return 0;





}





//video.cpp





char selection (char n)





{


switch(n)


{


case 'a' : case 'A':


cout%26lt;%26lt;'; case A';;


break;





case 'b': case 'B':


return 0;





default:


cout%26lt;%26lt;';Please select the appropriately la.';;


}


}


C++, help me check my coding! ?
You were missing a semi-colon a couple of lines up. The line that was defining the function ';selection';.


You have a lot of unnecessary lines in the code. As examples, you have #include ';video.cpp';, which is this file itself. You shouldn't do a recursive include of the implementation file itself.


You missed passing a parameter to the ';selection'; function when it was actually used.


Also, you missed a few spaces in the code. Please try and use adequate spacing not only to make the code more readable, but easier to debug where errors are.


Here is the corrected code that I tested compilation with:





// video.cpp





#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


using namespace std;





char selection(char n);





main()


{


int pwd;


char n;





system(';CLS';);


cout %26lt;%26lt; ';\t .:: Welcome to AJ Video Store System ::.'; %26lt;%26lt; endl;


cout %26lt;%26lt; endl;





cout%26lt;%26lt;';Enter your password: ';;


cin%26gt;%26gt;pwd;





if (pwd = 1234)


{


cout %26lt;%26lt; ';Successful log in!'; %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; endl;





cout %26lt;%26lt; ';\t .:::: Video Store System Menus ::::. '; %26lt;%26lt; endl;


cout %26lt;%26lt; endl;





cout %26lt;%26lt; '; A) Add a new video record '; %26lt;%26lt; endl;


cout %26lt;%26lt; '; B) Exit '; %26lt;%26lt; endl;





cout %26lt;%26lt; endl;


cout %26lt;%26lt; endl;





cout %26lt;%26lt; ';Please enter your menu: ';;


cin %26gt;%26gt; n;





system(';CLS';);





n = selection(n);


}





else


{ cout %26lt;%26lt; '; Incorrect Password!'; %26lt;%26lt; endl;


}





return 0;





}





//video.cpp





char selection (char n)


{


switch(n)


{


case 'a' : case 'A':


cout %26lt;%26lt; '; case A';;


break;





case 'b': case 'B':


return 0;





default:


cout %26lt;%26lt; ';Please select the appropriately la.';;


}


}


C++, help me check my coding! ?
So you are calling your function that you named selection... that you said would return a char... and you are setting a char you named n to be the result of your function call. 2 flaws I see with that.





First, your function doesn't return the right data type(not intuitively anyways) So fix that. Either declare it as returning an int, or change what it returns or change it to void and return nothing.





Second, where the real problem with your program is, you said selection would take one argument. Exactly one argument. Yet you never give it one argument. So how can it know what selection the user actually made?





// pass argument


selection(n);





// fix function declaration(in both places)


void selection(char n)





EDIT





Make that 3 errors then. You don't have a semicolon after your function header.





Also, save yourself future headaches and organize your includes more consistently.





#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include ';header.h';


#include ';video.cpp';


using namespace std;





Put the function header for your selection function in header.h, or after your includes. If video.cpp uses this function, put it in header.h You would then #include ';header.h'; in both source files.
include';video.cpp';





char selection(char n);





//mainVideo.cpp





#include%26lt;iostream%26gt;


#include%26lt;cstdlib%26gt;


using namespace std;





#include';header.h';





main()


{


int pwd;


char n;





cout%26lt;%26lt;';\t .:: Welcome to AJ Video Store System ::.';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';Enter your password: ';;


cin%26gt;%26gt;pwd;





if (pwd = 1234)


{


cout%26lt;%26lt;';Successful log in!';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';\t .:::: Video Store System Menus ::::. ';%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;'; A) Add a new video record ';%26lt;%26lt;endl;


cout%26lt;%26lt;'; B) Exit ';%26lt;%26lt;endl;





cout%26lt;%26lt;endl;


cout%26lt;%26lt;endl;





cout%26lt;%26lt;';Please enter your menu: ';;


cin%26gt;%26gt;n;





system(';CLS';);





n = selection();


}





else


{ cout%26lt;%26lt;'; Incorrect Password!';%26lt;%26lt;endl;


}





return 0;





}





//video.cpp





char selection (char n)





{


switch(n)


{


case 'a' : case 'A':


cout%26lt;%26lt;'; case A';;


break;





case 'b': case 'B':


return 0;





default:


cout%26lt;%26lt;';Please select the appropriately la.';;


}


}
try correcting the statement:n = selection();


to: selection(n);
What's the error message?

No comments:

Post a Comment