patterncppMinor
Reading input from keyboard
Viewed 0 times
inputreadingfromkeyboard
Problem
I am required to read the following text from a keyboard (
-
A line beginning with the
-
The first two allocations are physical memory size and number of generations.
-
A global bracket will be opened and it may be followed by a line called
where
-
-
The
I have the following code that somewhat takes care of this. I want to know how to improve on it.
```
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using std::stringstream;
string pMem,sGen, comment,val,input,input_for,id_size,id,init_str1, init_str2, inc_str, id_dummy,s_out,sss, id_dummy1;
int gen=0, pMem_int=0,i=0, gBrckt =0,cBrckt=0, oBrckt=0, id_size_int,v1,v2, for_oBrckt=0,for_cBrckt=0,y=0, y1=0, g=0;
unsigned long pMem_ulong =0, id_size_ulong;
char t[20], m[256], init1[10],init2[10],inc[10];
unsigned pos_start, pos,pos_strt=0,pos_end=0;
string extract(string pMem_extract);
unsigned long toByte(int pMem_int_func, string val);
void commentIgnore(string& input);
void func_insert();
void func_insert_for();
stringstream out;
stdin). Please note that it will be entered by the user from the keyboard in this format only.#the total size of physical memory (units are B, KB, MB, GB)
512MB 2 #the following are memory allocations
{
abc = alloc(1KB);
{
y_ = alloc(128MB);
x1= alloc(128MB);
y_ = alloc(32MB);
for (i = 0; i < 256; i++) abc[i] =alloc(512kB);
x1 = alloc(32MB); x2 = alloc(32MB); x3 = alloc(32MB);
x1.next = x2, x2.next = x3, x3.next = x1;
}
abc = alloc(256MB);
}-
A line beginning with the
# sign is considered a comment and is ignored.-
The first two allocations are physical memory size and number of generations.
-
A global bracket will be opened and it may be followed by a line called
abc = alloc(1KB);where
abc is the object name and 1KB is the memory size allocated.-
x1.next = x2, where x1 points to x2.-
The
for loop is entered in this format and it can have a same-line command or can have nested for loops.for (i = 0; i < 256; i++) abc[i] =alloc(512kB);I have the following code that somewhat takes care of this. I want to know how to improve on it.
```
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using std::stringstream;
string pMem,sGen, comment,val,input,input_for,id_size,id,init_str1, init_str2, inc_str, id_dummy,s_out,sss, id_dummy1;
int gen=0, pMem_int=0,i=0, gBrckt =0,cBrckt=0, oBrckt=0, id_size_int,v1,v2, for_oBrckt=0,for_cBrckt=0,y=0, y1=0, g=0;
unsigned long pMem_ulong =0, id_size_ulong;
char t[20], m[256], init1[10],init2[10],inc[10];
unsigned pos_start, pos,pos_strt=0,pos_end=0;
string extract(string pMem_extract);
unsigned long toByte(int pMem_int_func, string val);
void commentIgnore(string& input);
void func_insert();
void func_insert_for();
stringstream out;
Solution
-
With this much code, you definitely should avoid using
-
You have a lot of global variables, and you shouldn't have any. They're generally discouraged as they can be modified anywhere within the program. This can introduce bugs, and it'll just make maintenance more painful. Only use them when you have no other choice.
Otherwise, in this procedural code, just pass to variables to functions as needed. That'll limit the variables scopes to which entity needs them, and it'll vastly improve your code. Based on the rest of the code, it looks like you've declared most of the variables in global. This is either lack of awareness of global variables, or some kind of laziness with handling variables.
-
Keep your use of indentation and whitespace consistent. Four spaces is customary for indentation, and there should be whitespace between operators and after commas.
-
Don't leave commented-out code present; it just clutters your code. If there's a specific reason for doing that in certain places, specify that; otherwise, leave it out.
-
Instead of having all those function prototypes, have
-
-
For the input blocks in
Each of those different blocks are done three times, so have a
Also, since you have
-
In
-
It doesn't make sense to call
Moreover, since this is done in
With this much code, you definitely should avoid using
using namespace std. More info about that can be found here.-
You have a lot of global variables, and you shouldn't have any. They're generally discouraged as they can be modified anywhere within the program. This can introduce bugs, and it'll just make maintenance more painful. Only use them when you have no other choice.
Otherwise, in this procedural code, just pass to variables to functions as needed. That'll limit the variables scopes to which entity needs them, and it'll vastly improve your code. Based on the rest of the code, it looks like you've declared most of the variables in global. This is either lack of awareness of global variables, or some kind of laziness with handling variables.
-
Keep your use of indentation and whitespace consistent. Four spaces is customary for indentation, and there should be whitespace between operators and after commas.
-
Don't leave commented-out code present; it just clutters your code. If there's a specific reason for doing that in certain places, specify that; otherwise, leave it out.
-
Instead of having all those function prototypes, have
main() defined below each of the functions. That way, they'll already be recognized by main() when you need to call them from there.-
void functions don't need an explicit return at the end; they'll always return there. However, you'll still need it if you need to exit from the function prematurely.-
For the input blocks in
main(), have each different one in a different loop, rather than typing out each one. Don't Repeat Yourself (DRY).Each of those different blocks are done three times, so have a
for loop for each one (using the first as an example):for (int i = 0; i > pMem;
}
}Also, since you have
std::cin and std::getline together, you should have std::ignore between them here, and in other similar instances.-
In
toByte, there's no need for C-style casts. Use C++-style casts here, specifically with static_cast<>:You don't need a C-style cast here:pMem_ulong = static_cast(pMem_int_func);-
It doesn't make sense to call
exit(0) after reporting an error. The 0 argument indicates successful termination, whereas 1 indicates failure.Moreover, since this is done in
main(), just use return 1.Code Snippets
for (int i = 0; i < 3; i++)
{
if(pMem == "#") {
cin.clear();
pMem.clear();
getline(cin,comment);
cin >> pMem;
}
}pMem_ulong = static_cast<unsigned long>(pMem_int_func);Context
StackExchange Code Review Q#26410, answer score: 5
Revisions (0)
No revisions yet.