Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can someone tell me what am i doing wrong in this code?
New Posts  All Forums:Forum Nav:

Can someone tell me what am i doing wrong in this code?

post #1 of 6
Thread Starter 
This is one of my class project and im recently learning about functions... I'm doing this project and i think im doing it correctly i am using visual studio 2010 and i keep getting an "expected declaration" on my last return why is this? the section is in bold to easily identify what im talking about. Also why are my variables coming up in red like i have an error ( i underlined the variables that are coming back to me like an error). I thought i already declared them correctly, or did i not? Can someone look at this coding ( which is not complete) and let me know what do i need to do in order for the errors to go away?




#include <iostream>
#include <iomanip>
using namespace std;

// Start Function prototypes

void inputData(int&, int&, int&, int&);
int calcNewBalance(int, int ,int);
void displayOutput(int, int, int, int, int);

// End function prototypes

// Start Main

int main (void)
{
// Local Declarations

int ID, inventory, received, sold, newbal;

// Program Statement

inputData(ID, inventory, received, sold) ;
newbal = calcNewBalance(inventory, received, sold);
displayOutput(ID, inventory, received, sold, newbal);
return 0;

} // End Main

// Start inputData Function

void inputData(int& ID, int& inventory, int& received, int& sold)
{
cout << " Please enter Book ID #: " ;
cin >> ID ;
cout << " Please enter starting Inventory #: " ;
cin >> inventory ;
cout << " Please enter how many books received: " ;
cin >> received ;
cout << " Please enter how many books sold: " ;
cin >> sold ;
return ;

} // End inputData function

// Start calcNewBalance function

int calcNewBalance( int inventory, int received, int sold)
{

// Local Declaration

int newbal;

// Program Statement

newbal = inventory + received - sold ;
return newbal ;


}
Edited by Aknolidge - 3/31/11 at 12:52pm
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #2 of 6
I could maybe help more if you posted more of the code or is this all that youve done so far?

The problem might be the references you used in your input data function(the &'s). I am not 100% sure at the moment, I have a monster headache
(hope my bump brings someone else here)
if you still cant fix the program msg me later.
Edited by L3gacy - 3/31/11 at 1:47pm
The Time Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Kentsfiel Intel Core 2 Quad Q6700 3.6Ghz Evga nForce 790i SLI FTW Edition EVGA GTX 260 Core 216 Corsair DDR3 4096Mb @ 1600Mhz 
Hard DriveOptical DriveOSKeyboard
1 TB Western Digital LG SuperMulti Dvd Burner Windows 7 Ultimate 64Bit Razr Lycosa 
PowerCaseMouse
ANTEC True Power 750 Watt@80 Bronze Rating NZXT Guardian 921 Logitech M510 
  hide details  
Reply
The Time Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Kentsfiel Intel Core 2 Quad Q6700 3.6Ghz Evga nForce 790i SLI FTW Edition EVGA GTX 260 Core 216 Corsair DDR3 4096Mb @ 1600Mhz 
Hard DriveOptical DriveOSKeyboard
1 TB Western Digital LG SuperMulti Dvd Burner Windows 7 Ultimate 64Bit Razr Lycosa 
PowerCaseMouse
ANTEC True Power 750 Watt@80 Bronze Rating NZXT Guardian 921 Logitech M510 
  hide details  
Reply
post #3 of 6
- Disregard -

I apologize for botching your thread.
Edited by Bradeno - 3/31/11 at 2:29pm
post #4 of 6
Your function prototypes should have the same parameter list as your function definitions.

For example:

Your prototype
Code:
void inputData(int&, int&, int&, int&);
should look like this
Code:
void inputData(int& ID, int& inventory, int& received, int& sold);
iCode
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K ASUS P8Z68 Deluxe 2X EVGA DS GTX 580 Superclocked SLI CORSAIR Vengeance 8GB (2x4GB) 
Hard DriveOptical DriveOSMonitor
OCZ Vertex 3 MAX IOPS Edition ASUS DRW Win 7 Ultimate x64 HP ZR30w 
KeyboardPowerCaseMouse
Logitech Ultra-Thin Illuminated Keyboard COOLER MASTER Silent Pro Gold Series 1000W LIAN LI PC-B25FWB Black Logitech MX518 
  hide details  
Reply
iCode
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K ASUS P8Z68 Deluxe 2X EVGA DS GTX 580 Superclocked SLI CORSAIR Vengeance 8GB (2x4GB) 
Hard DriveOptical DriveOSMonitor
OCZ Vertex 3 MAX IOPS Edition ASUS DRW Win 7 Ultimate x64 HP ZR30w 
KeyboardPowerCaseMouse
Logitech Ultra-Thin Illuminated Keyboard COOLER MASTER Silent Pro Gold Series 1000W LIAN LI PC-B25FWB Black Logitech MX518 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by Aknolidge View Post
This is one of my class project and im recently learning about functions... I'm doing this project and i think im doing it correctly i am using visual studio 2010 and i keep getting an "expected declaration" on my last return why is this? the section is in bold to easily identify what im talking about. Also why are my variables coming up in red like i have an error ( i underlined the variables that are coming back to me like an error). I thought i already declared them correctly, or did i not? Can someone look at this coding ( which is not complete) and let me know what do i need to do in order for the errors to go away?

Code:
#include <iostream>
#include <iomanip>
using namespace std;

void inputData(int&, int&, int&, int&);
int calcNewBalance(int, int ,int);
void displayOutput(int, int, int, int, int);

int main (void)
{ 
    int ID, inventory, received, sold, newbal;

    inputData(ID, inventory, received, sold) ;
    newbal = calcNewBalance(inventory, received, sold);
    displayOutput(ID, inventory, received, sold, newbal);
    return 0;
}

void inputData(int& ID, int& inventory, int& received, int& sold)
{
    cout << " Please enter Book ID #: " ;
    cin >> ID ;
    cout << " Please enter starting Inventory #: " ;
    cin >> inventory ;
    cout << " Please enter how many books received: " ;
    cin >> received ;
    cout << " Please enter how many books sold: " ;
    cin >> sold ; 
    return ;
} 

int calcNewBalance( int inventory, int received, int sold)
{
    int newbal;
    newbal = inventory + received - sold ;
    return newbal ;
}
Just an FYI the comments you have are not that useful and actually make the code harder to read. Comments should explain what the code is doing, especially if it's not obvious (that said this is a school assignment and your professor could have explicitly told you to comment that way. If so comment like that but know that it's bad).

Now as to your problem, I don't see anything wrong with your code, I tried compiling the code you posted in GCC on windows and it compiles correctly with no warnings (after commenting out references to displayOutput ). I would guess that displayOutput is defined below the code you posted and that something is wrong with its declaration. Check it for misspelling, proper number and type of inputs, etc.




Quote:
Originally Posted by SiPex View Post
Your function prototypes should have the same parameter list as your function definitions.

For example:

Your prototype
Code:
void inputData(int&, int&, int&, int&);
should look like this
Code:
void inputData(int& ID, int& inventory, int& received, int& sold);
As long as the correct variable types are in the prototype, having the variable name explicitly there does not matter in C++.
Edited by The Bartender Paradox - 3/31/11 at 2:52pm
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #6 of 6
If it's still not working. Trying printing out the values of ID, inventory, received, and sold before you call your calcNewBalance function to make sure they are getting read correctly. Everything looks okay to me, but does it matter if your parameter is (int &ID) rather than (int& ID)?
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can someone tell me what am i doing wrong in this code?