How to count the number of characters entered?
My assignment is
In C++, use pointer to count the number of characters user entered. Print the output on screen
Here is what I have done. I do not know how to count the characters I entered.
#include <iostream>
using namespace std;
int main()
{
int string[20] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int sum = 0;
char a;
cout << "Enter less than 20 characters(no space) : ";
for (int i = 0; i < 20; i++)
{
cin >> a;
string[i] = a;
if (string[i] != 0)
sum = sum + 1;
else
break;
}
cout << endl;
cout << "The number of characters in " << *string << " is " << sum;
return 0;
}
c++ pointers
add a comment |
My assignment is
In C++, use pointer to count the number of characters user entered. Print the output on screen
Here is what I have done. I do not know how to count the characters I entered.
#include <iostream>
using namespace std;
int main()
{
int string[20] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int sum = 0;
char a;
cout << "Enter less than 20 characters(no space) : ";
for (int i = 0; i < 20; i++)
{
cin >> a;
string[i] = a;
if (string[i] != 0)
sum = sum + 1;
else
break;
}
cout << endl;
cout << "The number of characters in " << *string << " is " << sum;
return 0;
}
c++ pointers
3
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
4
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
You have a variable namedstring- but it's an array ofints, which doesn't in any way resemble a string.
– Igor Tandetnik
Nov 25 '18 at 15:55
BTW, the usual definition of a string is zero or more characters, notint.
– Thomas Matthews
Nov 25 '18 at 18:20
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22
add a comment |
My assignment is
In C++, use pointer to count the number of characters user entered. Print the output on screen
Here is what I have done. I do not know how to count the characters I entered.
#include <iostream>
using namespace std;
int main()
{
int string[20] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int sum = 0;
char a;
cout << "Enter less than 20 characters(no space) : ";
for (int i = 0; i < 20; i++)
{
cin >> a;
string[i] = a;
if (string[i] != 0)
sum = sum + 1;
else
break;
}
cout << endl;
cout << "The number of characters in " << *string << " is " << sum;
return 0;
}
c++ pointers
My assignment is
In C++, use pointer to count the number of characters user entered. Print the output on screen
Here is what I have done. I do not know how to count the characters I entered.
#include <iostream>
using namespace std;
int main()
{
int string[20] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int sum = 0;
char a;
cout << "Enter less than 20 characters(no space) : ";
for (int i = 0; i < 20; i++)
{
cin >> a;
string[i] = a;
if (string[i] != 0)
sum = sum + 1;
else
break;
}
cout << endl;
cout << "The number of characters in " << *string << " is " << sum;
return 0;
}
c++ pointers
c++ pointers
edited Nov 25 '18 at 12:57
Swordfish
1
1
asked Nov 25 '18 at 12:42
user10702014user10702014
1
1
3
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
4
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
You have a variable namedstring- but it's an array ofints, which doesn't in any way resemble a string.
– Igor Tandetnik
Nov 25 '18 at 15:55
BTW, the usual definition of a string is zero or more characters, notint.
– Thomas Matthews
Nov 25 '18 at 18:20
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22
add a comment |
3
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
4
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
You have a variable namedstring- but it's an array ofints, which doesn't in any way resemble a string.
– Igor Tandetnik
Nov 25 '18 at 15:55
BTW, the usual definition of a string is zero or more characters, notint.
– Thomas Matthews
Nov 25 '18 at 18:20
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22
3
3
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
4
4
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
You have a variable named
string - but it's an array of ints, which doesn't in any way resemble a string.– Igor Tandetnik
Nov 25 '18 at 15:55
You have a variable named
string - but it's an array of ints, which doesn't in any way resemble a string.– Igor Tandetnik
Nov 25 '18 at 15:55
BTW, the usual definition of a string is zero or more characters, not
int.– Thomas Matthews
Nov 25 '18 at 18:20
BTW, the usual definition of a string is zero or more characters, not
int.– Thomas Matthews
Nov 25 '18 at 18:20
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22
add a comment |
1 Answer
1
active
oldest
votes
From my understanding of the requirements, you'll need to use a pointer.
int main()
{
static const size_t MAX_CHARS = 20;
char text[MAX_CHARS] = {0};
std::cout << "Enter up to 20 characters, no spaces: ";
size_t quantity;
char c;
while (std::cin >> c)
{
if (quantity >= MAX_CHARS) break;
text[quantity++] = c;
}
// Count the characters using a pointer:
if (quantity < MAX_CHARS)
{
text[quantity] = '';
}
else
{
text[MAX_CHARS - 1] = '';
}
quantity = 0;
char * pointer = &text[0];
while (*pointer != '')
{
++quantity;
++pointer;
}
std::cout << "Characters entered: " << quantity << "n";
return 0;
}
I guess this is an exercise to understand pointers.
Otherwise, I suggest using std::string and std::string::length().
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467545%2fhow-to-count-the-number-of-characters-entered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From my understanding of the requirements, you'll need to use a pointer.
int main()
{
static const size_t MAX_CHARS = 20;
char text[MAX_CHARS] = {0};
std::cout << "Enter up to 20 characters, no spaces: ";
size_t quantity;
char c;
while (std::cin >> c)
{
if (quantity >= MAX_CHARS) break;
text[quantity++] = c;
}
// Count the characters using a pointer:
if (quantity < MAX_CHARS)
{
text[quantity] = '';
}
else
{
text[MAX_CHARS - 1] = '';
}
quantity = 0;
char * pointer = &text[0];
while (*pointer != '')
{
++quantity;
++pointer;
}
std::cout << "Characters entered: " << quantity << "n";
return 0;
}
I guess this is an exercise to understand pointers.
Otherwise, I suggest using std::string and std::string::length().
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
add a comment |
From my understanding of the requirements, you'll need to use a pointer.
int main()
{
static const size_t MAX_CHARS = 20;
char text[MAX_CHARS] = {0};
std::cout << "Enter up to 20 characters, no spaces: ";
size_t quantity;
char c;
while (std::cin >> c)
{
if (quantity >= MAX_CHARS) break;
text[quantity++] = c;
}
// Count the characters using a pointer:
if (quantity < MAX_CHARS)
{
text[quantity] = '';
}
else
{
text[MAX_CHARS - 1] = '';
}
quantity = 0;
char * pointer = &text[0];
while (*pointer != '')
{
++quantity;
++pointer;
}
std::cout << "Characters entered: " << quantity << "n";
return 0;
}
I guess this is an exercise to understand pointers.
Otherwise, I suggest using std::string and std::string::length().
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
add a comment |
From my understanding of the requirements, you'll need to use a pointer.
int main()
{
static const size_t MAX_CHARS = 20;
char text[MAX_CHARS] = {0};
std::cout << "Enter up to 20 characters, no spaces: ";
size_t quantity;
char c;
while (std::cin >> c)
{
if (quantity >= MAX_CHARS) break;
text[quantity++] = c;
}
// Count the characters using a pointer:
if (quantity < MAX_CHARS)
{
text[quantity] = '';
}
else
{
text[MAX_CHARS - 1] = '';
}
quantity = 0;
char * pointer = &text[0];
while (*pointer != '')
{
++quantity;
++pointer;
}
std::cout << "Characters entered: " << quantity << "n";
return 0;
}
I guess this is an exercise to understand pointers.
Otherwise, I suggest using std::string and std::string::length().
From my understanding of the requirements, you'll need to use a pointer.
int main()
{
static const size_t MAX_CHARS = 20;
char text[MAX_CHARS] = {0};
std::cout << "Enter up to 20 characters, no spaces: ";
size_t quantity;
char c;
while (std::cin >> c)
{
if (quantity >= MAX_CHARS) break;
text[quantity++] = c;
}
// Count the characters using a pointer:
if (quantity < MAX_CHARS)
{
text[quantity] = '';
}
else
{
text[MAX_CHARS - 1] = '';
}
quantity = 0;
char * pointer = &text[0];
while (*pointer != '')
{
++quantity;
++pointer;
}
std::cout << "Characters entered: " << quantity << "n";
return 0;
}
I guess this is an exercise to understand pointers.
Otherwise, I suggest using std::string and std::string::length().
answered Nov 25 '18 at 18:35
Thomas MatthewsThomas Matthews
44.6k1174123
44.6k1174123
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
add a comment |
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
But after I entered your program, the results it shows are always“Characters entered: 0”
– user10702014
Nov 28 '18 at 13:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467545%2fhow-to-count-the-number-of-characters-entered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Use a string directly. And display its length. (or say what the problem is, you can't display a integer array like this).
– Matthieu Brucher
Nov 25 '18 at 12:44
4
If you have questions about homework assignments, your teacher or instructor is the best resource for getting help. That's what they're being paid for, that's their job.
– Sam Varshavchik
Nov 25 '18 at 12:45
You have a variable named
string- but it's an array ofints, which doesn't in any way resemble a string.– Igor Tandetnik
Nov 25 '18 at 15:55
BTW, the usual definition of a string is zero or more characters, not
int.– Thomas Matthews
Nov 25 '18 at 18:20
Also, the assignment says to use a pointer. You don't have any pointers in your code. Definitely talk with the instructor and get some clarification.
– Thomas Matthews
Nov 25 '18 at 18:22