I'm trying to test the logic for an assignment, but I'm not sure if what I have will work. I have declared float variables, but in the actual program it will be random input float variables. The goal is if A>B then divide A/B, else A*B.
Here is the compiler output below, but I don't understand the "unresolved external symbol _main referenced in function ___tmainCRTStartup" error. Otherwise it looks like it will work if I can get past this.
----- Build started: Project: Testing If-then, Configuration: Debug Win32 ------
Compiling...
Testing If-then.cpp
c:\users\dell01\documents\visual studio 2008\projects\testing if-then\testing if-then\testing if-then.cpp(31) : warning C4715: 'calc' : not all control paths return a value
Linking...
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Users\Dell01\Documents\Visual Studio 2008\Projects\Testing If-then\Debug\Testing If-then.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Users\Dell01\Documents\Visual Studio 2008\Projects\Testing If-then\Testing If-then\Debug\BuildLog.htm"
Testing If-then - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
if (first > second )
{
output=first/second;
}
else
{
output=first*second;
}
// or you can do an eze if/else
(first>second) ? output=first/second : output=first*second;
either ways doesn't really matter, seems like what you have is ok.... kinda hard to tell because you don't have code blocks around your code. there's one closing bracket for the first if statement missing, so basically you have a nested if, which probably should be an if/else or if / if else...
[ code ] ....code.... [ /code ]
(without the spaces ^^)
why should it be in main if it is a function of another class?
personally I rarely have anything in main, if anything simply calling functions of other classes.
Most of the time people only post sample output on here, so you shouldn't assume their not using int main, unless otherwise specified.
usually what I do is keep looping an object until the user wants to quit or until the program has run a certain amount of times or something. here's an example:
// assume I have all the includes and using's
int runProgram();
int main()
{
// this could be a boolean value if you wish, or a char or whatever...
// but if it is just remember to change the return type in your function.
constint QUIT= -2;
int userChoice =0;
while ( userChoice != QUIT )
{
userChoice = runProgram();
}
return 0;
}
int runProgram()
{
// do you program logic in here, if statements etc..
// now we want to loop a response till we get a Y or N
char userChoice; // we can use the same variable name here because it is in a different scope.
do
{
cout << "would you like to continue? Y / N: ";
cin >> userChoice;
if ( userChoice == 'N' )
{
return -2; // -2 is returned which is same value as QUIT, so program exits.
}
elseif ( userChoice != 'Y' )
{
cout << "invalid, option" << endl;
}
} // loop keeps asking the question "do you want to continue" until a Y or N is entered.
while (( userChoice != 'N' ) || ( userChoice != 'Y' )) ;;
return 0; // because 0 is returned, only in the case user enters 'Y' the program starts over...
}
This is still pretty clumsy and if the user types a string then we get a lot of repeat output we don't want, but because I have been plugging away at java for the last 3 weeks, and just started another side project, I can't think off the top of my head how to grab the first char in the buffer. Also it should ignore case when comparing, but without quickly looking it up I can't recall :P sorry, but you get the logic behind it at least ?
check the string class, on this website and you should get a good idea how to grab the first char, maybe the best idea is instead of making userChoice a char, it could be a string, then it doesn't matter what the user inputs because you can just grab the first letter by using
if (userChoice.substring(0).ignoreCase() != 'Y') <---- will have to check that syntax, but it should be something like that...