Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this.

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

Header contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}

I wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this.

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

Header contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}

I wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this.

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

Header contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}
deleted 16 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Assignment: Customizing output in accordance to the input

WantedI wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this. Thanks!

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

P.S.: HeaderHeader contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}

Assignment: Customizing output in accordance to the input

Wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this. Thanks!

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

P.S.: Header contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}

Customizing output in accordance to the input

I wanted to know if I overdid it and maybe could have kept this code simpler and cleaner. I need to execute this assignment:

Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly, and converts them to decimal form. Then your program should output the values in properly spaced columns like this:

 0x4 hexadecimal converts to 67 decimal
 0123 octal converts to 83 decimal
 65 decimal converts to 65 decimal

Now the code compiles and does what it's intended to do, but I still need to get into that programmer state of mind where you keep only the stuff that really matters, to the last letter (thus keeping the code simple, succinct and efficient). Showing me what I can get rid of and revise would bring me closer to achieving this.

#include"..\..\Header.h"
void print_number(const string& concatenation, const string& type) {
 istringstream s(concatenation);
 int number = 0;
 if(type == "hexadecimal") s >> hex >> number;
 if (type == "octal") s >> oct >> number;
 else s >> number;
 cout << concatenation << '\t' << type << "\tconverts to " << dec << number << " decimal.\n";
}
int main() {
 while (cin) {
 string da;
 cin >> da;
 if (da[0] == '0') {
 if (da[1] == 'x') print_number(da, "hexadecimal");
 else print_number(da, "octal");
 }
 else print_number(da, "decimal");
 }
 system("pause");
 return 0;
}

Header contains these lines (just in case):

 #include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
 throw runtime_error(a);
}
Added necessary language tag
Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73
edited title
Link
C.H.
  • 75
  • 1
  • 6
Loading
Source Link
C.H.
  • 75
  • 1
  • 6
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /