Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with C++ / complete programming noob here
New Posts  All Forums:Forum Nav:

Help with C++ / complete programming noob here

post #1 of 5
Thread Starter 
I bought the book " C++ primer plus" on the Kindle store last night. I'm on chapter two and already stuck with the end of chapter samples.

The example asks you to:
Write a program that asks the user to enter an hour value and a minute value. The main() function should then pass these two values to a type void function that displays the two values in the format shown in the example run.

Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28

Here is the code I have right now and I don't know where to go.

I guess what i don't know how to do is "send" the hour and minute values into the second function and then return them together. Remember, I literally starting learning programming yesterday.
Code:
//time.cpp -- displays time after usr input
#include <iostream>

void time(int);
        
        int main()
        {
                using namespace std;
                cout << "Enter the hour: ";
                int hour;
                cin >> hour;
                cout << "Enter the minutes: ";
                int minutes;
                cin >> minutes;
                time(hour minutes);
                return 0;
        }


        void time(int n)
        {
                using namespace std;
                cout << hour << ":" << minutes << endl;
        }
SFG II
(17 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 Asus P6T Sapphire Radeon 6850 6GB Crucial DDR3 1333 
Hard DriveHard DriveHard DriveOS
OCZ Vertex 2 HITACHI Deskstar 5K3000 SAMSUNG EcoGreen F4 Linux Mint 12 
OSOSMonitorKeyboard
Windows 7 Home premium Backtrack 5 Dual 22" Samsung SyncMaster 2253LW Saitek Eclipse 
PowerCaseMouse
PC Power&Cooling Silencer 610 NZXT Phantom Black Logitech M505 Wireless 
  hide details  
Reply
SFG II
(17 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 Asus P6T Sapphire Radeon 6850 6GB Crucial DDR3 1333 
Hard DriveHard DriveHard DriveOS
OCZ Vertex 2 HITACHI Deskstar 5K3000 SAMSUNG EcoGreen F4 Linux Mint 12 
OSOSMonitorKeyboard
Windows 7 Home premium Backtrack 5 Dual 22" Samsung SyncMaster 2253LW Saitek Eclipse 
PowerCaseMouse
PC Power&Cooling Silencer 610 NZXT Phantom Black Logitech M505 Wireless 
  hide details  
Reply
post #2 of 5
Functions take arguments(parameters) these tell the function what to use. In your example, you want the time function to do something with an hour value and a minute value. so you just pass the variables when you call the function. Now, you need to set up your time() function to take these variables. So you'll change your time function declaration to void time(int hour, int minutes). Now your time function will accept two integers, the first one is the hour value and the second variable is the minute value.

Now you want to call the time(int h, int m) from the main function, to do this, you type the name of the function and then the parameters you want to pass separated by commas. So now to call the time function from the main function you would do time(hour,minutes) and now you've just passed the two variables to the time functions.
FermIntel Inside
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k 5Ghz 24/7 ASUS Maximus IV Extreme Gen 3 GTX 470 SLI + HD5450 16GB G.Skill Ripjaws 
Hard DriveOptical DriveOSMonitor
lots none Win7 Ultimate (b:7600) LG W2353V + LG W2343T + LG W2254TQ 
KeyboardPowerCaseMouse
Apple Aluminum Seasonic SS 850HT Sunbean ACTS Razer Deathadder 3500dpi 
  hide details  
Reply
FermIntel Inside
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k 5Ghz 24/7 ASUS Maximus IV Extreme Gen 3 GTX 470 SLI + HD5450 16GB G.Skill Ripjaws 
Hard DriveOptical DriveOSMonitor
lots none Win7 Ultimate (b:7600) LG W2353V + LG W2343T + LG W2254TQ 
KeyboardPowerCaseMouse
Apple Aluminum Seasonic SS 850HT Sunbean ACTS Razer Deathadder 3500dpi 
  hide details  
Reply
post #3 of 5
Thread Starter 
Thanks very much. I must've not paid much attention to the book because I don't recall the book telling I could use commas to separate parameters.

This is the code I have now and it works fine.
Code:
//time.cpp -- displays time after usr input
#include <iostream>

void time(int,int);
        
        int main()
        {
                using namespace std;
                cout << "Enter the hour: ";
                int hour;
                cin >> hour;
                cout << "Enter the minutes: ";
                int minutes;
                cin >> minutes;
                time(hour,minutes);
                return 0;
        }


        void time(int hour, int minutes)
        {
                using namespace std;
                cout << "The time is: " << hour << ":" << minutes << endl;
        }
SFG II
(17 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 Asus P6T Sapphire Radeon 6850 6GB Crucial DDR3 1333 
Hard DriveHard DriveHard DriveOS
OCZ Vertex 2 HITACHI Deskstar 5K3000 SAMSUNG EcoGreen F4 Linux Mint 12 
OSOSMonitorKeyboard
Windows 7 Home premium Backtrack 5 Dual 22" Samsung SyncMaster 2253LW Saitek Eclipse 
PowerCaseMouse
PC Power&Cooling Silencer 610 NZXT Phantom Black Logitech M505 Wireless 
  hide details  
Reply
SFG II
(17 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 Asus P6T Sapphire Radeon 6850 6GB Crucial DDR3 1333 
Hard DriveHard DriveHard DriveOS
OCZ Vertex 2 HITACHI Deskstar 5K3000 SAMSUNG EcoGreen F4 Linux Mint 12 
OSOSMonitorKeyboard
Windows 7 Home premium Backtrack 5 Dual 22" Samsung SyncMaster 2253LW Saitek Eclipse 
PowerCaseMouse
PC Power&Cooling Silencer 610 NZXT Phantom Black Logitech M505 Wireless 
  hide details  
Reply
post #4 of 5
in your time function...you want to pass both your hour and minute variables...also just type "using namespace std" at the top once....instead of all over your code.
Code:
//time.cpp -- displays time after usr input
#include <iostream>
using namespace std;

 void time(int hour, int minutes);
        
        int main()
        {
                
                cout << "Enter the hour: ";
                int hour;
                cin >> hour;
                cout << "Enter the minutes: ";
                int minutes;
                cin >> minutes;
                time(hour, minutes);
                return 0;
        }


        void time(int hour, int minutes)
        {
                
                cout << hour << ":" << minutes << endl;
        }
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by Super Coffee View Post

Thanks very much. I must've not paid much attention to the book because I don't recall the book telling I could use commas to separate parameters.
This is the code I have now and it works fine.

Great! Hope you like C++
FermIntel Inside
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k 5Ghz 24/7 ASUS Maximus IV Extreme Gen 3 GTX 470 SLI + HD5450 16GB G.Skill Ripjaws 
Hard DriveOptical DriveOSMonitor
lots none Win7 Ultimate (b:7600) LG W2353V + LG W2343T + LG W2254TQ 
KeyboardPowerCaseMouse
Apple Aluminum Seasonic SS 850HT Sunbean ACTS Razer Deathadder 3500dpi 
  hide details  
Reply
FermIntel Inside
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k 5Ghz 24/7 ASUS Maximus IV Extreme Gen 3 GTX 470 SLI + HD5450 16GB G.Skill Ripjaws 
Hard DriveOptical DriveOSMonitor
lots none Win7 Ultimate (b:7600) LG W2353V + LG W2343T + LG W2254TQ 
KeyboardPowerCaseMouse
Apple Aluminum Seasonic SS 850HT Sunbean ACTS Razer Deathadder 3500dpi 
  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 › Help with C++ / complete programming noob here