Validate logic on if-then structure

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.

#include "stdafx.h"
#include <iostream>

using namespace std ;


int calc ()
{
float first, second, output ;
char ch ;

first = 10 ;
second = 5 ;
output = 0 ;

if (first > second)
{
output = first / second ;

if (first < second)
{
output = first * second ;

cout << output << endl ;
cout << "Press 1 and 'Enter' to exit." << endl ;
cin >> ch ;
return 0 ;
}
}

}


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 ==========
Last edited on
how about

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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 ^^)
Last edited on
is this a function named calc or what???? or should this be int main() instead of int calc().

Either way I think that may be giving the error. But I think you shold use an if else like above. Then see what you get
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.
Last edited on
gcampton and btripp. Thank you for your response. I see what I did wrong and corrected it.

This snippet is part of a three part a simple program assignment: input, calc, and output.
It was not very easy for me, but I have a clue.

Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

// 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.
   const int 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.
      }
      else if ( 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 ?
Last edited on
gcampton,

Once again, thanks for your insight. I've been digging around the web too for other options and yours is consistent is what I've seen.
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...
Topic archived. No new replies allowed.