-
October 27th, 2014, 11:25 AM
#1
quick question
Hi i want to fix this code.
The program must accept 7 values or stop it when you type s.
The code not only couts the element, but also some other random numbers.
Any help would be appreciated, thanks.
Code:
#include<iostream>
using namespace std;
int main()
{
int numb[7];
int i;
char s;
for (i=0;i<=6;i++)
{
cout << "Please enter number:";
cin >> numb[i];
while (i=='s');
}
for (i=0;i<=6;i++){
cout <<endl<<numb[i]<<endl;
}
system("pause");
return 0;
-
October 27th, 2014, 11:35 AM
#2
Re: quick question
Originally Posted by dk_SAM123
Hi i want to fix this code.
The program must accept 7 values or stop it when you type s.
The code not only couts the element, but also some other random numbers.
Any help would be appreciated, thanks.
Code:
#include<iostream>
using namespace std;
int main()
{
int numb[7];
int i;
char s;
for (i=0;i<=6;i++)
{
cout << "Please enter number:";
cin >> numb[i];
while (i=='s');
}
for (i=0;i<=6;i++){
cout <<endl<<numb[i]<<endl;
}
system("pause");
return 0;
Your loops are wonky. Your while loop accomplishes nothing. Even if it did, i is an int and generally shouldn't be compared to a char. In your case, i starts at 0 and is incremented till it reaches 7. It could never equal s. Read up on the break statement and figure out what it is that should equal s.
You want to cout the number of numbers input, which may not necessarily be the size of your array.
-
October 27th, 2014, 01:06 PM
#3
Re: quick question
The numbers to input are of type int, 's' is of type char. If you try to input a type char when type int is expected then the input stream state will be set to the fail state and no further input will be possible in the program until the fail state has been cleared.
If you want to input integers and also be able to enter s to stop entering then one way would be to obtain input as a string and test whether either the string is s or the string is numerical (ie a number has been entered). If s then the loop is terminated otherwise convert the entered string digits to a number.
Another way would be to simply assert that entering any non-integer will stop the input rather than just an s. In this case one way would be
Code:
for (i = 0; i < 7 && (cout << "Please enter number:") && (cin >> numb[i]); i++);
Also note that when dealing with arrays, good practice would be to declare the size of the array as a const int and then use this value to declare arrays, iterate them etc.
Code:
const int arrsize = 7;
int numb[arrsize];
for (i = 0; i < arrsize && (cout << "Please enter number:") && (cin >> numb[i]); i++);
It is also usual to have < arrsize rather than <= arrsize -1.
ie < 7 rather than <= 6.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|