add size

i need a code which add the size words , when user enter words eg : if enter "hello" is 5 and the next words "hi" is 2 the add (hello +hi) equal 7, when i have 10 , user receive the message "capacity full"

1
2
3
4
5
6
7
8
  string str;
    do {
        cout << "Enter text: ";
        getline (cin,str);
        cout << "You entered: " << str<<str.size()<<'\n';

    } while (str.size() != 10);
    cout<<"capacity full"<< endl;

thank's you for your help
you are overwriting str every loop, so hello is 5 and then you overwrite, hi is 2 and so str.size = 2.
you have 2 choices. you can append to the string (do you want spaces?) or keep a running total of the entries.

a running total looks like
int rt = 0;
do{ ... etc
...
getline (cin,str);
rt+= str.size();
...
}while (rt <= 10)
Hello chrichri,

You could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	int total{};
	std::string str, str2;

	do
	{
		cout << "Enter text: ";
		getline(cin, str);
		cout << "You entered: " << str << str.size() << '\n';

		total += str.size();

	} while (total < 11);

	str2 = str.substr(10, 0);

	std::cout << "\n Capacity full" << std::endl;

	std::cout << "\n the string is: " << str2 << std::endl;

Keep in mind that this should work, but I did not test it.

Hope that helps,

Andy
Words or lines? You do read lines. Is that what you should do?

If I (the user) type "ho hum", is that "one word" of 6 characters, or a 2-char and a 3-char word, 5 in total?


Do you care about the words, or just about the sum of their sizes?
it's done
but when ii add vector because i want store the element done in buffer until certain size
like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string str;
   vector<uint8_t> buffer;
   int rt =0;
      int dt =0;
  const char*src=str.data();
   char *dest= new char[buffer.size()];
    do {
        cout << "Enter text: ";
        getline (cin,str);
        cout << "You entered: " << str<<str.size()<<'\n';
        rt+= str.size();
       memcpy(dest,src,strlen(src)+1);
       dt+= buffer.size();
        cout<< rt << endl;

cout<< dt << endl;
    } while (dt <= 10);


but dt show 0
Last edited on
keskiverto (7737) i want care about the words.

i know that this program just the sum of their sizes
.
I don't see anything added to buffer so when you add dt and buffer together it would be: 0 += 0

Talemache~
which is the good writing ? i'm reaally lost
Hello chrichri,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

This program would work much easier in C++. Mixing C and C++ does work, but not the best idea.

Using what you started with and making some changes I came up with this:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <vector>

int main()
{

	int rt = 0;
	int dt = 0;
	int counter{ 1 };

	std::string str;

	//const char*src = str.data();  //  <--- Has not use here or value at this point.
	
	std::vector<int> buffer;

	std::string dest;

	do
	{
		std::cout << "\n Enter text: ";
		getline(std::cin, str);

		std::cout << "\n You entered: " << str << " which has a length of " << str.size() << '\n';
		rt += str.size();

		buffer.emplace_back(str.size());

		dest = str;

		dt = buffer.size();

		std::cout << "\n Running total of word length = " << rt << std::endl;

		std::cout << "\n Total size of vector = " << dt << std::endl;
	} while (rt <= 10);

	std::cout << "\n Capicity is full." << std::endl;
	
	std::cout << '\n';

	for (auto lc : buffer)
	{
		std::cout << " \"buffer\" element " << counter << ". " << lc << '\n';
		counter++;
	}
        // <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

You defined a vector, but never put anything into it. At least this code will put something into the vector although the vector is never used after it is created. I added the for loop to print the contents of the vector just so you can see what is there.

Hope that helps,

Andy
thank's a lot

However i don't understand why you don't use vector<unit8>buffer ;
i tried with vector<unit8_t> i receive the binary it s possible to convert in char or int
Thank s a lot


i use "memcpy" because i want to make a copy in my buffer
What is unit8_t?
The "memcpy" is hardly ever the appropriate way of doing copying in C++.

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

int main()
{
	std::string word;
	std::vector<std::string> words;

	size_t count {};
	while ( count < 10 && std::cout << "enter word: " && std::cin >> word )
	{
		count += word.size();
		words.emplace_back( word );
	}
	std::cout << "\n Capacity is full. " << count << "\n\n";

	for ( auto w : words )
	{
		std::cout << " \"buffer\" element " << w << ". " << w.size() << '\n';
	}
}
vectors can just be assigned, x = y style. C arrays memcpy is good to copy them, but its generally not so good to use C arrays excessively in c++. Let it do the memory movement for you.

uint8_t is unsigned char. Due to how binary works, you can pretty much assign char and unsigned char to each other so long as you really didnt care about the sign of the value and are using them as just characters. It will warn you, but it won't hurt anything. chars in turn are just 1 byte integers.

so, in short (bad pun), you can directly convert these. It is NOT safe to copy an int to a character if the int is bigger than 1 byte's worth of data, it will not fit and the data will be damaged. It is safe to copy characters (uint8 etc) to integers though. And it is safe to copy uint8 char unsigned char int8 byte (these are all 1 byte entities) to each other (again, as letters or binary data it just works, as actual numeric values or if using bitwise logic you must watch the sign of them).
Topic archived. No new replies allowed.