how do i diplay characters from array without the wrest?

here is my output

Enter a sentence.
i want the code to stop here->
i want the code to stop here-> ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
Press any key to continue . . .




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;
const int SIZE = 500;

void main()
{
	char Sentence[SIZE];
	int i;

	cout << "Enter a sentence. \n";

	cin.getline(Sentence,SIZE);

	for (i = 0; i < SIZE ; i++)
	{
		cout << Sentence[i];
	}
	cout << endl;
}
Print "Sentence" entirely.
Store "Sentence" as a std::string and use std::getline(cin,Sentence).
Use strlen() instead of SIZE to compute length.

(Only one of the above)
can i use strlen() in a for loop?
Yes you can, but it isn't advised... as strlen needs to iterate over the entire string to calculate it's length -- and if you're doing that for each character in the string, you're doing a lot of unnecessary work.

Though really... is there any reason why you're printing this one character at a time? Why not just print the string directly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;
const int SIZE = 500;

int main() // <- note:  int main... never void
{
	char Sentence[SIZE];
	int i;

	cout << "Enter a sentence. \n";

	cin.getline(Sentence,SIZE);

	cout << Sentence << endl;  // <- just print the string normally
}


Or... better yet... don't use char arrays, and use an actual string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>  // <- for string

using namespace std;
// const int SIZE = 500; <- don't need this anymore.  With string,
//    we can have strings of any size

int main() // <- note:  int main... never void
{
	string Sentence; // <- make this a string instead of a char array
	int i;

	cout << "Enter a sentence. \n";

	getline(cin,Sentence); // <- use string form of getline

	cout << Sentence << endl;  // <- print the string normally
}
closed account (z1CpDjzh)
@noobb programer
You can put anything in a loop

Code 1:

strlen code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

void main()
{
	char Sentence[500];
	int i;

	cout << "Enter a sentence. \n";

	cin >> Sentence;

	for (i = 0; i < strlen(Sentence) ; i++)
	{
		cout << Sentence[i];
	}
	cout << endl;
}


code 2:

String code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main()
{
	string Sentence;
	int i;

	cout << "Enter a sentence. \n";

	getline(std::cin, Sentence);

	cout << Sentence;

	cout << endl;
}
kinda both:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main()
{
	string Sentence;
	char Sentence2[500];
	int i;
	stringstream SS; 

	cout << "Enter a sentence. \n";

	getline(std::cin, Sentence);

	SS << Sentence;
	SS >> Sentence2;



	cout << Sentence2;

	cout << endl;
}

closed account (j3Rz8vqX)
Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
//#include <cstring>
using namespace std;
const int SIZE = 500;

int main()
{
	char Sentence[SIZE];

	cout << "Enter a sentence. \n";

	cin.getline(Sentence,SIZE);

	//Sentence[strlen(Sentence)] = '\0';

	cout<<Sentence<<endl;//<--- Should work
	return 0;
}
Last edited on
Sentence[strlen(Sentence)] = '\0';

This line is pointless. strlen works by looking for the null terminator... so this line is basically writing a null to what already must be null.
@TheGentlmen: Cache the result of strlen (And make "i" more local):
1
2
size_t len = strlen(Sentence);
for(size_t i = 0; i < len; ++i)
Last edited on
closed account (z1CpDjzh)
@EssGeEich I never said it was optimized best code... I just put it together in notepad.... But thanks i would have never caught that problem.
Topic archived. No new replies allowed.