New to C++ and programming...stuck on programming project

Hi.

I am new to the world of C++ and programming, i have been self studying it for approx. 3 weeks now and am working on a programming project and am stuck on some certain aspects, such as how to assign AM to char& A and PM to char& P. Another thing i am having trouble with is when i enter in a minute which is within the range zero to nine, the output gives me: 12:5 instead of 12:05. How do i get the 0 to display in front of the five.

The following is the programming project description from the textbook i have:
"Write a program that converts from twenty-four-hour notation to twelve-hour notation. For example, it should convert 14:25 to 2:25PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and one for the output. Record the AM/PM information as a value of type char, 'A' for AM and 'P' for PM. Thus, the function for doing the conversion will have a call-by-reference formal parameter of type char to record whether it is AM or PM. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program."

This is the code i have written so far:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Program converts from 24hr time to 12hr time.
#include <iostream>
using namespace std;

void introduction();
//Postcondition: Description of program is written on the screen.

void input_time(int& hours, int& minutes, char& A, char& P);
//Precondition: User enters time correctly.
//Postcondition: The value of hours has been set to the
//the variable 24hr_time.

int time_conversion(int& hours, int& minutes, char& A, char& P);
//Precondition: Obtains hours, minutes, A and P from input_time function.
//Postcondition: converts time from 24hr time to 12hr time and subtracts 12 
//hours if hour is greater then 12.

void output_time(int& hours, int& minutes, char& A, char& P);
//Postcondition: Outputs the converted time.

int main()
{
	
	int _24hr_time, minutes;
	char ans, AM, PM;
	
	introduction();
	do
	{
	input_time(_24hr_time, minutes, AM, PM);
	time_conversion(_24hr_time, minutes, AM, PM);
	output_time(_24hr_time, minutes, AM, PM);
	
		cout << "Peform another conversion?"
			 << " (Type y of yes or n for no): ";
		cin >> ans;
	} while (ans == 'y' || ans == 'Y');
	
	return 0;
}

//Uses iostream:
void introduction()
{
	cout << "This program converts 24 hour time to 12 hour time.\n";
}

//Uses iostream:
void input_time(int& hours, int& minutes, char& A, char& P)
{
	cout << "Enter the hour portion of the 24 hour time you wish to convert to 12 hour time: ";
	cin >> hours;
	cout << "Enter the minutes portion of the 24 hour time you wish to convert to 12 hour time: ";
	cin >> minutes;
}

//Uses iostream:
int time_conversion(int& hours, int& minutes, char& A, char& P)
{
	{
		if (hours > 12)
			hours = hours - 12;
		return 0;
	}
}

//Uses iostream:
void output_time(int& hours, int& minutes, char& A, char& P)
{
		cout << "The converted time is: " << hours << ":" << minutes << endl;
}



As mentioned earlier i am new and self studying so i don't have a teacher giving me feedback so i welcome any feedback from you guys and gals and will be greatly appreciated. If you can see any way of making this program more efficient by reducing the size of the code, if you spot any bad habits i'm starting to pick up or if there are errors in my programing methodology, please let me know :)

Thanks
Dalhousie12
I think you may have your syntax mixed up. You can not assign 2 letters to a single character. You wrote char& A. In order to get a string of characters use std::string or char*.

To get the program to print out a zero before the minute use a simple if statement.
1
2
3
4
5
6
7
if(minutes < 10) 
{
    cout << "..." << hours << ":0" << minutes << endl;
    return;
}

cout << "... normal";
Last edited on
Thanks.

I was able to get the program to print out a zero before the minute.

I am only at chapter 4 of the textbook i have and Chapter 10 is about strings. I know very little about strings, so i don't understand anything you said about my syntax being messed up.

What does std::string mean? What does it do? What does the scope resolution operator do? I know the term and know what it looks like (::), but i don't understand what it does.
A cleaner/more appropriate solution to the leading zero would be to format the output using cout's width and fill parameters.

The scope resolution operator does just what it says, resolves scope, depending on the context it's used in.

It is used to define the methods declared in a class when that definition occurs outside the classes declaration. This tells the compiler what class that method belongs to.

It can be used to access variables of the same name defined in different scopes. If you have a global variable called x, and a local variable called x, by default you access the local variable. You have to use :: if you want to access the global variable (though in general if you're running into a situation like this, it's probably a result of poor design)

It is also used to access objects defined in a namespace. All the STL objects are defined under the namespace std. There are two ways to access these objects. The first you're already using in your code. using namespace std; sets the scope to that namespace, allowing you to access all those objects without any additional specification. Without using namespace std;, you have to use the scope resolution operator to access any of those objects.
Last edited on
I suggest you learn about strings. They can be very useful if you get to know all of their functions.

http://www.cplusplus.com/reference/string/
How can you know all these void functions and those stuff in only 3 weeks?..
4 hours average x 5 each night = 20 x 3 = 60 hours of studying
16 hours average per weekend x 3 = 48 hours of studying
Total = 108 hours studying in approx 3 weeks.

Some of it is rote memorization and some of it i know and understand.

i will try to do the same, but it´s too many hours per day... i'de have to exclude Counter-Strike from my schedulexDD
No one learns how to program in three weeks. You may learn the syntax but it takes time. You don't have to study C++ everyday for weeks on end. If you read a chapter a day and mess around with the new coding techniques/keywords for a few days, that is fine. If you don't understand concepts try to get more information on them.

What I have been told to do was learn the language at home and take the class at school to test your knowledge.
Last edited on
Thanks for the advice eker676. I do agree that no one learns how to program in three weeks and understand i have lots more to learn and understand. I wasn't implying i know how to program with the above post. I was only referring to concepts in the code i posted in my original post and that some of it is rote memorization and some of the concepts i understand.

i learn c++ through tuturials on net, or on youtube,... whats the best way to learn it? sometimes it gets boring when reading books...prehaps practice on programming helps
You are not required to read the book for hours on end. The best way to learn C++ IMO is to make programs.
yes, but sometimes just wont have any ideas to program xD, but its the coolest way of learning (:
btw, whats IMO?
In my opinion. ;)

Here is a link for some programming projects you could try.
http://www.cplusplus.com/forum/beginner/3473/


Thanx, youre awesome x)
Topic archived. No new replies allowed.