is there something wrong with array.size? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I want to give in a string into an array from keybord, so that i can count how many letters it has with for loop
I tried to buil and run the code but i doesnt work actually and there an error but I dont really understand the error and the way to fix them. The error is :
request for member size in Bao( my Array), which is of none class type
Here is my code:
char Bao[100];
cout<<"Give me a sentence"<<endl;
cin.getline(Bao, 100, '\n');
cout<< Bao.size()<<endl;

There is no function 'size' in a c++ array. You have to use
sizeof(array)
which gives the size of the array in bytes. To get the true size of the array use
sizeof(array)/sizeof(array[0])
which divides the size of the array with the size of a single element in the array giving you the number of elements.
Also, why are you using an array in this instance? It seems like a string might be what you are looking for.

Here, you shouldn't even be using an array. Use std::string.
Here's some sample code that accomplishes the same thing as the code you posted:
std::string sentence;
std::cout << "Give me a sentence" << std::endl;
std::cin.getline(sentence, 100, '\n');
std::cout << "Scentence is " << sentence.size() << " bytes" << std::endl;
EDIT: using getline() for the spacebar bug.

Related

Code for decrypting simple sentences made with CodeBlocks crashes without outputting a single character [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am making a program that inputs the alphabetical order for encrypting a sentence to decrypt it (just replacing characters). I am required to input the number of words it has, but the method I'm using doesn't need that so I just input it and do nothing with it (it doesn't matter because an AI will be verifying only the output of the program). I think I came up with a decent algorithm, but the program keeps crashing after I enter the encrypted sentence. I tried changing a lot of things but nothing seems to solve the issue. Also, I'm sorry for any things that don't make sense in the code, I'm just a beginner. Please help me stop the program from crashing so I can submit it before the end of tomorrow.
This is my code
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, i, j;
string key, encrypted;
cin >> key;
char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
cin >> n;
getline(cin>>ws, encrypted);
for (i=0;i<=encrypted.size();i++)
{
for (j=0;j<encrypted.size();j++)
{
if (encrypted[i]==key[j])
{
cout << alphabet[j];
}
else if (encrypted[i]==key[j]+32)
{
cout << alphabet[j]+32;
}
else if (encrypted[i]==' ')
{
cout << " ";
}
}
}
}
Your program has the following bugs:
First bug:
As already pointed out in the comments section, you should change the line
for (j=0;j<encrypted.size();j++)
to
for (j=0;j<key.size();j++)
Second bug:
Due to integer promotion, the expression alphabet[j]+32; will evaluate to a value of type int, not char. This means that
cout << alphabet[j]+32;
will print a number, not a character. Therefore, you must cast the result of the expression back to the data type char, like this:
cout << static_cast<char>( alphabet[j]+32 );
Third bug:
The line
cout << " ";
should only be called once per outer loop iteration, i.e. once per character in the string encrypted. However, you are calling it once per inner loop iteration, which means you are calling it several times per character. Therefore, you should remove that statement from the inner loop and put it into the outer loop.
Fourth bug:
The line
for (i=0;i<=encrypted.size();i++)
is wrong. It should be < instead of <=. Otherwise, you will also process the terminating null character of the string, which is not meaningful.

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);

Add integers to array using cin simultaneously in C++ [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 5 years ago.
Improve this question
Hellow guys i want to add nine integers to array at once time without pressing enter key in run time. please guys tell me how to add nine integers to array simultaneously in C++.
Thanks!
If you want to process each integer value right after its input in console is complete (e.g. in that a blank indicates that the next integer value shall begin), you are in a bad position.
The reason is that terminal input (beyond of what your C++ program can influence) often is buffered, and even cin might not receive any character until Enter or EOF is pressed in the terminal.
There may exist workarounds like conio.h or ncurses, but the are not standard and probably not worth the effort in your situation unless you really need to implement integer scanning for a production environment tightly connected to console input.
Try it out and compare input taken directly from console to input from a stream that is already "filled" with enough input:
int main() {
stringstream ss("12 34 56 78 90 10 11 12 13");
//istream &in = ss; // would output each integer immediately.
istream &in = cin; // will probably wait for enter before processing begins.
int value = 0;
for (int i=0; i<9; i++) {
if (! (in >> value))
break;
cout << value << "; ";
}
}

C++ Calculator With Unlimited Inputs [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 4 years ago.
Improve this question
Hi I am a beginner to the c++ language and I would like to know if there is any way of writing a program with unlimited inputs. For example: I want to write a calculator program just to add numbers. That's easy enough but is there a way so that the user can add as many numbers as he wants without being asked how many numbers he wants. Such that if he wants to add three numbers he can just type "1+1+1" or if he wants to add four numbers he adds "+1" to the end of the previous line.Like this the user is not stuck to a fixed number of inputs or so he doesn't need to be asked how many inputs he wants. What functions in c++ do I need to know in order to do this
You can use a while loop to read from standard input. (std::cin) Some basic code to read from a while loop, and add the input to a sum is as follows:
#include <iostream>
#include <string>
#include <cstdlib>
int main(){
std::string line = "";
double sum = 0.0;
while(line != "end"){
std::cout<<"The current sum is: "<<sum<<std::endl;
std::cout<<"Enter the number you would like to add or \"end\" to exit: ";
std::cin>>line;
sum += atof(line.c_str());
}
std::cout<<"The final sum is: "<<sum<<std::endl;
}
This will read numbers until it receives the input "end".
For parsing and evaluating expressions that include infix operators, few things are simpler than the Shunting Yard Algorithm.
Implementing this in C++ is scarcely different than any other language with a container library (or built-in support) that provides stacks and queues. Here, you'll want to use std::stack and std::queue. The input to your program could be a single line (containing an expression typed by the user) read from std::cin (standard input, or the console) into an std::string.
This will not only permit expressions of any reasonable length, but also correctly handle arbitrary nesting of parenthesized sub-expressions, evaluation of special functions, and custom operators.
Yes. It is possible. You can use vector of ints. Get user's input and calculate sum of elements from vector. Put this in loop and that is what you wanted.
try this:
#include <iostream>
int main (int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
std::cout << "argument " << i << " is " << argv[i] << std::endl;
return 0;
}

Issue in file operations in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This is part of a c++ code that writes value of a vector of strings into a file.
int main () {
//freopen ("out.txt", "w+", stdout);
ofstream data;
data.open("data.txt");
BinaryTree<string>* bt = new BinaryTree<string>;
LoadBinaryTree(bt);
fillArrayOfNodes(bt);
for (int i = 0; drawArray[i] != "\0"; i++)
data << drawArray[i] << endl;
data.close();
delete bt;
return 0;
}
First, I couldn't write into the file. I mean after running the program and checking the output file, it was empty. after that, I noticed that my output format wasn't right. I changed it and now I can write into the file. (the code shown above is the modified code)
The problem is the way you're attempting to iterate through the array. The Standard C++ string class std::string should not be handled like a regular char array. That is, you shouldn't base your condition upon finding the null character. The correct way would be to iterate until you reach the length of the string.
Moreover, you should be using a vector of strings and inserting strings using push_back():
std::vector<std::string> v;
// fill vector with push_back()
for (int i = 0; i < v.size(); ++i)
data << v[i] << endl;
You need to include the right headers like <fstream>.
Try this example: http://www.cplusplus.com/doc/tutorial/files/