how to write correctly into multidimentional char array unknown amount of values but fixed amound of chars [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm sick and tired of solving why my ch[0] is of value "Thomas EdisonÇ#", when it should be "Thomas Edison"
int main(){
using namespace std;
ifstream in("U2.txt");
int n;
in>>n; //n=rows, so in every line there will be "name surname", time, money
char ch[n][21]; //I'm trying to get Name+Surname which must be 20 char long
in.read(ch[0], 20);
cout << ch[0]; //but getting Thomas EdisonÇ#
return 0;}
It works on one dimentional ch[21], but there's gonna be lots of values so I want to use ch[n][21]
Any other out of my box solution is welcome, I'm tired

You are forgetting that C strings need to be nul terminated
in.read(ch[0], 20);
ch[0][20] = '\0'; // add the nul terminator
cout << ch[0]; // now correct output

Related

How can I add letters in a sentence? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).

How to organize infinite loop with symbols analysis in it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to organize infinite loop with symbol analysis in it. In C I used fgets(buf, N, stdin), suppose buf is buf[10]. User could type string of any length and I could analyze it by breaking down the input and examining parts of length 10. How can I implement this in C++ without using C libraries. Sorry for my English if you can't understand what I mean
In C++ you should std::cin to read from standard input.
// #include <iostream>
do
{
char buf[10]{}; // create array of 10 bytes filled with zeros.
std::cin.read(buf, 10); // read 10 bytes
// at this point you should check if std::cin.read succeeded.
// otherwise you will be reading zeros.
std::streamsize numRead = std::cin.gcount(); // obtain number of read bytes.
std::cout << numRead << " " << buf << std::endl; // some printing.
}while(std::cin);

Making a password type program that needs to accept letter and number combinations? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am making a shopping list program. For this program, I need to be able to type in a user input that accepts both number (1564, 121,1, etc) and word (hello, goodbye, etc) combinations. The program reads numbers just fine, but it cannot process words. Thank you in advance. The part of the code I am stuck with is below:
int code, option, count = 0;
double quantity, price, cost;
string description;
cin >> code;
while ((code != 123456789) && (count < 2))
{
cout << "Incorrect code, try again \n";
cin >> code;
count++;
if (count == 2)
{
cout << "max # of tries reached. Goodbye. \n";
system("pause");
}
}
Your code variable is now an int. If you wanted that to be a string, declare it so: std::string code;. Note that you might need to #include <string> in the very beginning. Also, if you want to compare it with numbers, either you call something like atoi() (string has .cstr()), or better yet, you might just compare it with "123456789". HTH.

Deleting undesired characters at the end of a char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With this code below I dont know how to delete the undesired characters appearing at the end of the message array. It is compulsory for me to use char, can't use strings, because of the rest of my code.
recvbuf is also a char* recvbuf=new char
char* message=new char[140];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
printf("Message: %s\n", message);
delete[]recvbuf;
Though it is recommended you use strings to implement this code, the problem can be fixed by manually appending a null character \0 at the end of your char array.
You can introduce it as:
char* message=new char[141];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
message[140] = '\0'; //newly introduced line.
printf("Message: %s\n", message);
delete[]recvbuf;
NOTE 1: The size of the array was increased from 140 to 141 during initialization to make room for the \0 character at the end.
Cheers!

Accessing data in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).