New Posts  All Forums:Forum Nav:

"Grammatical" errors

post #1 of 8
Thread Starter 
At lines 20 I'm getting the errors "expected primary-expression before "else"" and "expected ';' before "else"" and at line 44 I keep getting the error "in function 'double calcMonthlyPayment(double. int, double, double)' expected ';' before ')' token. I know these are just "grammatical" errors so to speak but I for the life of me can't see what I'm doing wrong. Any help? Edit: This is in c++ btw. Sorry forgot to mention that
Code:
#include <iostream>
#include <cmath>
using namespace std;
double calcMonthlyPayment(double l, int time, double p, double m);
int main ()
{double l, m, p;
 int time;
     cout << "Please enter loan amount" << endl;
     cin >> l;
     
     if (l != 0)
     {cout <<"Please enter years to pay" << endl;
     cin >> time;
     cout << "Please enter the annual interest percentage as a whole number" << endl;
     cin >> p;}
     
     calcMonthlyPayment(l, time, p,m);
     
     cout << "Your monthly payment will be: " << m << endl;
     else
         break;

     
     system("pause");
     return 0;
}
double calcMonthlyPayment(double l, int time, double p, double m)
{    
       time= time * 12;
       m = (((p/1200)*pow((1+p/1200),time))/(pow((1+p/1200),time))-1))*l;
       return m;
}

Edited by Damaging Excess - 3/26/13 at 11:29am
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #2 of 8
Not commenting on the semantics of the code, just on the syntactic errors you're getting...

Your first error is occurring because you can't have code between your if- and else- blocks. You'll have to restructure your code by putting the two lines of code between the if,else blocks somewhere else, where it fits your logic. This is because if you have other code between the if and else blocks, the compiler won't know which if block you are referring to when it reaches the else block (since the else basically checks if the condition you have in the if statement is false).

Your second error is because you have an extra bracket after the second "time" in your assignment of m in the second function. It should be:
Code:
m = (((p/1200)*pow((1+p/1200),time))/(pow((1+p/1200),time)-1))*l;
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #3 of 8
Thread Starter 
Ok. I reworked it in a way where I had a similar program and I go it to loop properly as "ghettoly" (I'm making that a word if it isn't) as possible. My problem now though is that it's saying the break isn't part of the loop. I'm sort of running out of ideas to try. Needless to say, aggravating
Code:
#include <iostream>
#include <cmath>
using namespace std;
double calcMonthlyPayment(double l, int time, double p, double m);
int main ()
{double l, m, p;
 int time;
while (l != 0);
{
     cout << "Please enter loan amount" << endl;
     cin >> l;
     if (l == 0)
     {cout << "Thank you and have a good day \n" << endl;
           break;}
     else
     cout <<"Please enter years to pay" << endl;
     cin >> time;
     cout << "Please enter the annual interest percentage as a whole number" << endl;
     cin >> p;
     calcMonthlyPayment(l, time, p, m);
     cout << "Your monthly payment will be: " << m << endl;  
}
   
     system("pause");
     return 0;
}
double calcMonthlyPayment(double l, int time, double p, double m)
{    
       time= time * 12;
       m = (((p/1200)*pow((1+p/1200),time))/(pow((1+p/1200),time)-1))*l;
       return m;
}

Edited by Damaging Excess - 3/26/13 at 2:42pm
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #4 of 8
Ok first you have a few logical errors. When creating your while loop you terminate it. You have a semi colon at the end of your while loop which terminates it before it really begins. That will solve why it says your break point is not part of the code.

You also need to keep in mind your use of brackets. Your current setup has it so the only thing in the else statement is cout <<"Please enter years to pay" << endl; . I assume from the functionality of your code you need it around the remainder of the loop.

You should also initialize your variables. There is a chance that if you do not say what the value is that it could randomly be zero and your loop will break out instantly and make it seem like your program doesn't work.

Good Luck!
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #5 of 8
also you really should get rid of the break in your if statement, it isn't needed and is bad programming practice.

everything for the else statement needs to be in { } as well
Code:
#include <iostream>
#include <cmath>
using namespace std;
double calcMonthlyPayment(double l, int time, double p, double m);
int main ()
{
     double l = -1, m = 0, p = 0;
     int time = 0;

     cout << "Please enter loan amount" << endl;
     cin >> l;
     if (l == 0)
     {
          cout << "Thank you and have a good day \n" << endl;
     }
     else
     {
         cout <<"Please enter years to pay" << endl;
         cin >> time;
         cout << "Please enter the annual interest percentage as a whole number" << endl;
         cin >> p;
         m = calcMonthlyPayment(l, time, p, m);
         cout << "Your monthly payment will be: " << m << endl;
     }
  
     system("pause");
     return 0;
}
double calcMonthlyPayment(double l, int time, double p, double m)
{    
       time= time * 12;
       m = (((p/1200)*pow((1+p/1200),time))/(pow((1+p/1200),time)-1))*l;
       return m;
}


Edited by selectstriker2 - 3/26/13 at 8:11pm
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
post #6 of 8
Thread Starter 
Thank you. My biggest problem with coding so far is just making a lot of really dumb logical errors. I took the code selectstriker cleaned up for me and put it in a while loop so now it loops until 0 is pressed. And I won't be using break anymore because it's clearly easier to just put a cout statement with a message. I'm hoping through repetition I get it and stop making these dumb logical errors. +REP for both of you guys. Thank you again
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #7 of 8
Quote:
Originally Posted by selectstriker2 View Post

also you really should get rid of the break in your if statement, it isn't needed and is bad programming practice.

everything for the else statement needs to be in { } as well

There's nothing inherently wrong with a break statement inside an if, however in this case it's redundant(and won't ever be seen since the if is testing the same thing as the loop).

The code should be more akin to this:
Code:
#include <iostream>
#include <cmath>
using namespace std;

double calcMonthlyPayment(double l, int time, double p, double m);

int main ()
{
  double l = 0.0, m, p;
  int time;
  do {
     cout << "Please enter loan amount" << endl;
     cin >> l;
     cout <<"Please enter years to pay" << endl;
     cin >> time;
     cout << "Please enter the annual interest percentage as a whole number" << endl;
     cin >> p;
     calcMonthlyPayment(l, time, p, m);
     cout << "Your monthly payment will be: " << m << endl;  
  } while (l);

  cout << "Thank you and have a good day \n" << endl;
   
  system("pause");
  return 0;
}

double calcMonthlyPayment(double l, int time, double p, double m)
{    
       time= time * 12;
       m = (((p/1200)*pow((1+p/1200),time))/(pow((1+p/1200),time)-1))*l;
       return m;
}



Edited by Metmop - 3/26/13 at 11:08pm
Meh
(14 items)
 
   
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance 
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux 
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62 
MouseMouse Pad
Logitech MX 518 My desk. 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz 
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux 
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba 
MouseAudio
Touchpad Intel HD Audio 
  hide details  
Reply
Meh
(14 items)
 
   
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance 
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux 
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62 
MouseMouse Pad
Logitech MX 518 My desk. 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz 
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux 
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba 
MouseAudio
Touchpad Intel HD Audio 
  hide details  
Reply
post #8 of 8
glad we can help smile.gif I've been programming for over 10 years and I still learn things every day
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
ShadowForge
(12 items)
 
Defiant
(14 items)
 
CarbonCat
(13 items)
 
CPUMotherboardGraphicsRAM
Phenom II x6 1405T (unlocked Athlon II X4 640T) ASUS M5A99X EVO AM3+ Asus ENGTX470/2DI/1280MD5/V2 16 GB (4x4GB) G.Skill DDR3 1600 CAS9 1.35v 
Hard DriveHard DriveOptical DriveCooling
WD Caviar Blue 250 2.5" Laptop Drive WD Caviar Black 1TB 3.5" Pioneer BDR-203 BluRay Burner Corsair H60 push 
OSOSMonitorPower
Windows 7 Pro x64 Ubuntu 11.10 Samsung 40" 60hz  ANTEC NEO ECO 520W 
CaseMouse
NZXT Gamma Microsoft Bluetooth Notebook Mouse 5000 
CPUMotherboardGraphicsRAM
i7 - 2600k [5.0 1.42v] ASUS P8Z68 Deluxe Sapphire HD6950 2gb Dirt 3 Edition 8GB G.Skill DDR3 2133 CAS11 
Hard DriveOSMonitorKeyboard
60GB G.Skill Sniper + 2x1TB Spinpoint F3 Raid0 Win 7 Pro x64 ASUS VW266H Razer Blackwidow 
PowerCaseMouse
Seasonic X750 Gold Corsair Carbide 500R White G9 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming