code wanted

i have a question...

how to Develop a C++ application that performs the following tasks:
**Asks the user to input a data item of each of the following types: integer, decimal number of single precision, decimal number of double precision, short integer number, long integer number, unsigned integer number;
**Prints on the screen the number of memory bytes reserved for each data item;
**Prints on the screen the value and the address of each data item.

**waiting for your response
The way to do this is to think about the problem, write down every step that the program will need to do, and then convert each step into code.

Like Moschops said, 1 step at a time. Try doing just the first part that you have listed:

Ask user to input data for an integer. Complete this and only this, then do the next step. Before you know it you will have the satisfaction of a completed program.
The above advice is spot on. Nobody is going to go ahead and write the code for you. Step by step is the way forward.
That's my take on this,hope it helps,sry cuz it's a bit long though.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h" /*Required regarding .NET for the console log */
#include <iostream> /*Required for data input/output ( cin>>/cout<< ) */
#include <string>   /*Required to include strings ( words ) in the code */

using namespace std; /*The namespace used,in this case standard C++ (this has variable names and stuff ) */



//Declaration of a function to get the file size

void getSize (int a, float b,double c,short d,long e,unsigned f) {
	//Getting the size allocated to each data ( all size output will be an int number )
	int a1,b1,c1,d1,e1,f1;
	a1 = sizeof(a);
	b1 = sizeof(b);
	c1 = sizeof(c);
	d1 = sizeof(d);
	e1 = sizeof(e);
	f1 = sizeof(f);

	//Printing the size
	cout<<"int size is "<< a1<<" bytes"<<'\n'; /* '\n' is the same as endl */
	cout<<"float size is "<< b1<<" bytes"<<endl;
	cout<<"double size is "<< c1<<" bytes"<<endl;
		cout<<"short size is "<< d1<<" bytes"<<endl;
	cout<<"long size is "<< e1<<" bytes"<<endl;
	cout<<"unsigned size is "<< f1<<" bytes"<<'\n';
	//Exiting without return 0 because the type of this function is void (no data returned )
}

//Declaration of a function to print the values inserted

void varValues (int a, float b,double c,short d,long e,unsigned f) {
	//Printing the values inserted by the user
	cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f;
	//Exiting
}

	//Main program (this is where the app starts from )
int main() {

	//Declaration of variables
	
	int a;
	string x1,x2,x3,x4,x5,x6;
	float b;
	double c;
	short d;
	long e;
	unsigned f;
	int x;
		string y;
		//Asking the user to input all data
	cout<<"Enter integer"<<endl;
	cout<<"float"<<endl;
	cout<<"double float"<<endl;
	cout<<"short integer"<<endl;
	cout<<"long integer"<<endl;
	cout<<"unsigned integer"<<endl;

	//Assigning the data to variables
	
	 cin>>a;

 cin>>b;
		
 cin>>c;

 cin>>d;
	
 cin>>e;
	
 cin>>f;
	
	

	//Asking the user for next step
	A : cout<<"Enter 1 for data bytes,2 for variable values and 3 to exit"<<endl;

	//Getting the input
	cin>>x;
	//Introducing the cases,here you can also use if/else but switch is faster if you deal with multiple choices
	switch (x) {
	case 1 :
		getSize(a,b,c,d,e,f);
		//Pausing
		system("PAUSE");
		goto A;
		break;
	case 2 :
		varValues(a,b,c,d,e,f);
		system("PAUSE");
		goto A;
		break;
	case 3 :
		B :cout<<"Are you sure you want to exit?"<<endl;
		cout<<"Enter Y for yes and N for no"<<endl;
		cin>>y;
		if (y == "Y" ) {
			//Pausing for user to hit "Enter"
			system("PAUSE");
			//Exiting without errors
			return 0; }
		else if (y == "N" ) {
			/*Redirecting user */			goto A; }

		else cout<<"Unrecognised input"<<endl;
		goto B;
		break;
	default :
		cout<<"Unrecognised input"<<endl;
		goto A;
		break;
		//Exiting switch 
	} 

	cout<<"Have a nice day! :) ";
	return 0;
}
I think there's a few things wrong with this code:

1) That input section is going to cout a bunch of statements then ask the user to input without prompts. It should be cout, cin, cout, cin etc. This will be clearer to the user what they're entering.

2) There's a much better way of controlling the flow without using goto.

3) There's a bunch of unused variables.

4) Not all of the requirements are met. "Prints the value and the address of each data item".

5) The output is needlessly complex. It seems like there's prompts for what the user wants when the requirements state just to print. There's no point in making it more complex than it needs to be. It could be done in one statement per item:

 
cout << "Double. Value: " << my_dbl << "\tSize: " << sizeof(my_dbl) << " bytes\t Address: " << &my_dbl << endl;


Structuring it like point 5 also eliminates the need for the two functions in there and significantly shortens the code.


EDIT: Reading that back, it looks like I'm absolutely ripping your code apart. Not my intention, just trying to discourage bad habits.
Last edited on
Alright,I think I am sad enough to commit suicide now.Well,maybe not.I still want to go to the Moon first.Anyway,I know it is a bit complex but I wanted to make each function clear ( show the use of function ).I see what you are saying though.Thanks for helping me out,I am a starter too.The unused vars you are saying are the string x1 etc ones,yeah I kinda implemented a new function there but then I deleted it without deleting the vars,sry.
Yes you seem to have ripped my code completely so I will call you Ripper.
By the way,isn't the point of programming to always find a quicker way?I don't think there is anything you should be sorry about.
EDIT: Reading that back,it looks like your way gives all data but the user can't choose what he wants.i.e I only want to be given the size of a var, not it's address as well.So your way is a bit too prude.Not my intention,just trying to say my code has user options and yours does not,it just gives it all out.
EDIT2: Above in your reply you said no one was gonna post the code,but I did so you are wrong.
Also in there you said "step by step is the way forward" and that's what I did.But now you are telling me that it should be in "one statement per item " all together .Boy,you a heart breaker ;)
Last edited on
Haha, absolutely not my intention to make you feel bad about your code, and you have absolutely no reason to.

Anyway,I know it is a bit complex but I wanted to make each function clear ( show the use of function ).I see what you are saying though.

I understand, but sometimes is better to avoid using a function when it becomes contrived. For example, in the getSize function, there's a bunch of ints. They don't really need to be there, because you could do:

 
cout << sizeof(a); // Size of the passed in parameter 


So you don't need the ints to store the number of bytes. When you take away your ints, your left with a bunch of cout statements. It's probably easier to just do these in the main processing, rather than have a function with a lot of parameters.

Yes you seem to have ripped my code completely so I will call you Ripper.

Possibly the meanest nickname I've had. Still, though, no criticism is meant, only advice.

By the way,isn't the point of programming to always find a quicker way?

To an extent, yes. A lot of people go to great lengths to optimise code. But another point of programming is that there's always more than one way to do things. In this example, it's not a case of quickness, but a case of simplicity.

EDIT: Reading that back,it looks like your way gives all data but the user can't choose what he wants.i.e I only want to be given the size of a var, not it's address as well.So your way is a bit too prude.Not my intention,just trying to say my code has user options and yours does not,it just gives it all out.

I agree, your way would offer the user more choice. But it's important to pay attention to the program requirements. The only input it asks for is for the user to input each of the data types. It then asks to print stuff on the screen, but it doesn't ask for any more input. It's a little point, but if your on, for example, a university assignment, marks will be rewarded on things such as how the instructions have been followed as well as the technical stuff.

In general, you should never be sorry for your code; everyone starts somewhere. I've seen far, far worse code from far more experienced programmers in my time. :-)
Last edited on
Wow,thanks man.I really appreciate your time to help me ;)
I take that you must be a pro right?
I like the C++ language.What I would like to do is go beyond console apps and into windows apps.But I really don't know how to.I got Visual C++ 2010 Express but when I hit win32 project I think all the code there is Chinese.(No racism,I just don't know Chinese :) )
Could you hint me into a tut for such win32 apps?Sry for bothering you again Ripper,yeah I kinda like Ripper :D
Wow,thanks man.I really appreciate your time to help me ;)

Absolutely no problem at all.

I take that you must be a pro right?

Depends what constitutes a "pro". If you mean people pay me to program then yes. But I don't think you ever stop learning to code, so I'm always learning. :-)

Could you hint me into a tut for such win32 apps?

Depends. If it's Windows programming, you may want to look towards Windows Programming tutorials: http://msdn.microsoft.com/en-us/library/ff381398(v=vs.85).aspx

However, your username would suggest that your more interested in games, in which case I'd look at some graphics engines and frameworks. Here's a few:

HAAF: http://hge.relishgames.com/
SDL: http://www.libsdl.org/games.php
Allegro: http://alleg.sourceforge.net/
DirectX: http://www.directxtutorial.com/

Hope this helps.
Topic archived. No new replies allowed.