![]() |
|
C++ program - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: C++ program (/Thread-C-program--55333) Pages:
1
2
|
C++ program - BORW3 - 05-10-2014 Okay, I have a program I am supposed to make using only c++. It is supposed to allow the user to input int numbers and if the input is '0' the input process is terminated. Then the numbers that were inputed are summed up and displayed. Problems I had: -I tried to use Arrays to input but I don't have enough knowledge of how to input to a wide range of elements in an array. - Thaught I should use array pointers to save memory and allow excess input of about 1000 integers into the array. But same problem as above. Can someone please provide me with a solution. My program (I used eclipse on xubuntu with linux GCC compiler))Code: #include <iostream>
using namespace std;
int main()
{int * arr= new int[1000];
int a=1;
int j;
int i=0;
cout<<"Please input a number below, separate the values by space bar \n"
" Your values will be summed \n";
do{
cin>>arr[i];
}
while (arr[i]!=0);
cout<<arr[i]<<endl;
delete []arr;
return 0;
}Remember to use code tags for code RE: C++ program - dropzon3 - 05-10-2014 I'm not going to provide you a solution but let me give you a hints. You don't need to store all the numbers that have been input in order to reveal the sum at the end. Instead you should probably have some variable `int sum = 0;` and simply add the number taht was input to sum with code something like the following: *The following is not compilable code its just an example of what your code should do* Code: sum = 0;
temp = 0;
do:
cin>>temp;
sum = sum+temp; //or sum+=temp;
while (temp is not 0);
print sumRE: C++ program - BORW3 - 05-10-2014 (05-10-2014, 12:52 PM)dropzon3 Wrote: I'm not going to provide you a solution but let me give you a hints. Hmm, so simple, why didn't I think of it? let me try it RE: C++ program - ArkPhaze - 05-11-2014 Btw, the >> overload in this case has a very useful return, to help deal with invalid input. RE: C++ program - ArkPhaze - 05-11-2014 Here's how I'd go about doing it: Code: #include <iostream>
int main()
{
const size_t max_n = 100;
int arr[max_n] = {};
size_t index = 0;
int x = 0;
do
{
std::string junk;
while (!(std::cin >> x))
{
std::cout << "Invalid! Try again: ";
std::cin.clear();
std::getline(std::cin, junk);
}
if (x)
{
arr[index] = x;
++index;
}
}
while (index < max_n && x);
std::endl(std::cout);
std::cout << "Elements:\n";
for (const auto &it : arr)
{
if (!it) break;
std::cout << it << std::endl;
}
}RE: C++ program - BORW3 - 05-11-2014 Let me ask, if I use "using namespace std;" or "std::" which is better and why,? RE: C++ program - dropzon3 - 05-11-2014 (05-11-2014, 06:51 PM)BORW3 Wrote: Let me ask, if I use "using namespace std;" or "std::" which is better and why,? There is no performance difference between the two so it comes down to coding style. Personally I'd vote for always using std:: as having a habit of always specifying namespaces means you won't accidentally introduce ambiguity into your code that makes it harder for an outsider to understand. The exception to this being when you're actually developing code for a specific namespace then using 'using namespace ...' makes sense. So in the case of std I would advise to use std:: for clarity. RE: C++ program - ArkPhaze - 05-14-2014 (05-11-2014, 06:58 PM)dropzon3 Wrote:(05-11-2014, 06:51 PM)BORW3 Wrote: Let me ask, if I use "using namespace std;" or "std::" which is better and why,? Performance and coding style have hardly any relevance to the question IMO. "introduce ambiguity into your code that makes it harder for an outsider to understand" -- and the ambiguity to an outsider is a minor problem in comparison to name conflicts that allow the code to run in a different way than what was expected. The answer to this question also depends on where he's talking about putting 'using namespace std;' in his code. It would be perfectly find within a small body function, but would introduce a greater risk with a larger scope on its declaration. RE: C++ program - dropzon3 - 05-14-2014 Quote:Performance and coding style have hardly any relevance to the question IMO. Sure it does, if say "usinging namespame ..." was significantly faster than std:: then it would be advisable to always use that so performance certainly should be considered...in this case its meaningless but still considered. Coding style is certainly important also in decided which form is better because a clean coding style is better than a messy one. I take the question to be asking in general 'which is better' not just which is better for this particular individual case. I think that is where we differ. If its just about this specific code; who cares but if the question is more general coding style should be considered also...its hard to have clean code without also having consistent patterns. Quote:and the ambiguity to an outsider is a minor problem in comparison to name conflicts that allow the code to run in a different way than what was expected. Sure, most issues are minor compared to some larger issue...that doesn't mean you can't consider keeping your code readable to someone else even if other issues are more pressing. Surely any code is perfectly fine if it works... Quote:It would be perfectly find within a small body function, but would introduce a greater risk with a larger scope on its declaration. Build good habits from the start. Imo having a consistent style across big and small personal projects is a good thing. Write all code as if its going to become big and wild and maintained for a long time. Nothing wrong with following best practices even in the small scale. All that said I'm not a C++ dev; I rather dislike the language so I don't know what industry best practices are but using the namespace explictly seems like it would be a good practice. Just my two cents. https://www.thc.org/root/phun/unmaintain.html RE: C++ program - ArkPhaze - 05-14-2014 (05-14-2014, 04:29 AM)dropzon3 Wrote: Sure it does, if say "usinging namespame ..." was significantly faster than std:: then it would be advisable to always use that so performance certainly should be considered...in this case its meaningless but still considered. What is 'usinging namespame' exactly? :S ... Performance, as I'm trying to tell you, is out of the question. OP posted about that line of code, not programming in C++ in general. There are bad things that follow as a result of that line of code, but no effects to performance, therefore, there is no such comparable thing as to coding style or performance here. You've written out a whole bunch of off topic and very irrelevant stuff to what OP was asking. His question was on this line of code, you said yourself it's meaningless, so how in the world would it still be considered? Performance isn't even affected... That's like testing water for flammable characteristics when you already know it's properties... You made my point for me. (05-14-2014, 04:29 AM)dropzon3 Wrote: Coding style is certainly important also in decided which form is better because a clean coding style is better than a messy one. 'using namespace std;' is not related to coding style... Preference maybe, but that's not the same as coding style. Style has absolutely nothing to do with what declarations you make, but rather how you name your variables, what design paradigms you use, etc... (05-14-2014, 04:29 AM)dropzon3 Wrote: I take the question to be asking in general 'which is better' not just which is better for this particular individual case. I think that is where we differ. There's no difference in any case. I'm just pointing out that the risk is or can be less severe depending on where you use it. Do you understand what happens when you write that line in your source code or not? He specifically asked which was better to be using and why. You didn't answer anything. (05-14-2014, 04:29 AM)dropzon3 Wrote: Sure, most issues are minor compared to some larger issue...that doesn't mean you can't consider keeping your code readable to someone else even if other issues are more pressing. Surely any code is perfectly fine if it works... You're not a programmer are you? First, you started to argue about coding style and clean and concise code, now you're saying any code is fine if it works? You're also arguing about clean code and performance which has no bearing to the original question in this case over the real issues that any half-experienced C++ programmer would know about.. You didn't point any of them out. (05-14-2014, 04:29 AM)dropzon3 Wrote: Build good habits from the start. Imo having a consistent style across big and small personal projects is a good thing. Write all code as if its going to become big and wild and maintained for a long time. Nothing wrong with following best practices even in the small scale. You don't even know what good habits are because you're not a C++ programmer. You can't answer OP's question for him. This isn't a philosophical language, and C++ has many nuances that most don't understand at all. (05-14-2014, 04:29 AM)dropzon3 Wrote: Build good habits from the start. Imo having a consistent style All that said I'm not a C++ dev; I rather dislike the language so I don't know what industry best practices are but using the namespace explictly seems like it would be a good practice. Just my two cents. "seems like"? :S You're completely wrong btw.. I don't know why you're posting advice in this section when you have no idea about what that line of code does or what downsides it has when a programmer uses it over explicitly writing out the namespace specifiers... |