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"
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)
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:
#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.
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
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).