wrong printing 2D char array [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am doing my homework and I declared a 2D array but it's not printing what I want.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char NOTE[7][3] = { {'D','O'} , {'R','E'} , {'M','I'} , {'F','A'} , {'S','O','L'} , {'L','A'} , {'S','I'} };
for (int i = 0; i <7 ; i++)
cout <<setw(10)<< NOTE[i] << "\n";
return 0;
}
I want to get:
DO
RE
MI
FA
SOL
LA
SI
DO
but I got:
DO
RE
MI
FA
SOLLA
LA
SI
DO

NOTE[i] is an array of characters. << NOTE[i] inserts this array into a character stream. The array argument decays to a pointer to the first character.
The documentation says that behaviour is undefined unless the pointer operand points to a null terminated array. The array {'S','O','L'} is not null terminated. Therefore the behaviour of the program is undefined - although it could be argued that the behaviour of iterating across boundaries subarrays of a multidimensional array should be well defined (with the behaviour shown in your program), but strict interpretation of the standard is that doing so is UB.
So, to get the output you want, you either need to 1. null terminate each sub array (and therefore need a larger array), or to 2. print each character individually.

Related

Why am i getting wrong ans(rather 2 different answers) when comparing 2 strings [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When normally comparing the string this way:
#include <iostream>
using namespace std;
int main()
{
string a = "yb";
string b = "ya";
cout<<(a>b);
return 0;
}
result comes out 1. Which is right.
But when performing the same operation in this way:
#include <iostream>
using namespace std;
int main()
{
cout<<("yb">"ya");
return 0;
}
result is coming out 0.
How is this possible?
"yb" and "ya" have type char const[3]; they are arrays located somewhere in the memory containing the chars in the string literal and the terminating 0 char.
When doing ("yb">"ya") those objects decay to char const* and you're comparing pointers. Where the data is stored is compiler implementation defined and you cannot rely on the result.
To compare std::strings, you'd need to write
std::cout<<(std::string("yb")>std::string("ya"));
instead.

Pointer in c++for scanning an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
How to scan an array in c++ using pointer?
I have learned c programming .so i tried that way.
include<iostream>
using namespace std;
main()
{
int a[5],*p,i;
p=&a[5];
for(i=0;i<5;i++)
{
cout<<"enter"<<i+1<<endl;
cin>>(p+i);
}
for(i=0;i<5;i++)
{
cout<<*(p+i)<<endl;
}
}
I am expecting to scan using pointer just like in c programming
p=&a[5];
&a[5] is the address to one past the last element of the array. Incrementing this pointer, as well as indirecting through the pointer have undefined behaviour.
What you need is a pointer to the first element. You can use:
p = &a[0]; // this
p = a; // or this
Or you could simply not use p in the first place, and access a[i] directly.
cin>>(p+i);
This is wrong. p+i is a pointer. You cannot extract into an int pointer. You should extract into the integer object instead:
cin >> p[i];
cin >> a[i]; // or without p, as I pointed out above

Array not printing Properly in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
When running this program, if the value of n is set to 10 it will print the indices of array a and stored values from a[0] to a[9]. However if I set the value of n to more than 10 the then it only prints the indices of array a from a[0] to a[5] with their stored values. Can someone explain to me why this is happening?
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i, n=11;
for(i=0; i<n; i++) {
a[i]= 5;
}
cout<<"The array is: \n";
for(i=0; i<n; i++)
{
cout<<"a["<<i<<"] = "<<a[i]<<endl;
}
return 0;
}
If you increase the value of n past the size of the array (or equal to it indexing from 0), you are going past the end of the array. And going past the end of the array is undefined behavior. If your program is exhibiting undefined behavior. Anything can happen. See this blog post from Microsoft for more on undefined behavior
If you switch to an std::array instead of a C array and use .at(), then something well defined will happen, you will get an std::out_of_range exception. For more see http://en.cppreference.com/w/cpp/container/array/at and http://en.cppreference.com/w/cpp/container/array

Crashing Pointer Array C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Code::Blocks, Win7, C++
Hi, I'm creating an overview project for myself, and an integral part includes doing an Exchange Sort in descending order. I've created a simple trial run to test the concept.
Embarrassingly enough, I can't get past creating the array without the project crashing. This only happens when I create the array with pointers (My project's arrays are pointers, which is why I need this to work). The code compiles fine, but when I run it, I get a Window's notification that "ArrayTest.exe has stopped working"
When I print out each dereferenced value, I get:
"The original order: 1 13 5 7 2 "
When I print out each value's address, I get:
"The original order: 0x28ff08 0x28ffc4 0x760c8cd5 0x6aadab61 0xfffffffe "
The last address showed up when using an array length of 6, and crashes as well. Am I blind to a simple error in my code, or is this a hardware issue?
//filename ArrayTest
#include <iostream>
using namespace std;
int main()
{
int *j[5];
(*j)[0] = 1;
(*j)[1] = 13;
(*j)[2] = 5;
(*j)[3] = 7;
(*j)[4] = 2;
cout << "The original order: ";
for (int i = 0; i < 5; i++)
{
cout << (*j)[i] << " ";
}
return 0;
}
Consider int *j[5]; carefully. This is an array of pointers.
Those pointers are not pointing to memory that you own.
So the behaviour on dereferencing them is undefined.
Trivial fix: use int j[5]; instead, and j[0] = 1 etc.
When analysing code problems consider these numbers as approximating the probability of the error location:
99.0% - your code
0.9% - the standard library
0.09% - the compiler
0.009% - the operating system
0.001% - the hardware
If you want a pointer on array, you have to use this syntax
int array[5];
int (*j)[5] = &array;
(*j)[0] = 42;
So, firstly, the correct syntax is int j[5]; but that's C, not C++.
use std::array - actual C++ and with compile-time checking.
You're allocating an array of 5 pointers to int, not an array of 5 integers. Try to init the array like this: int j[5];
Your code writes your values 1, 13, 5, 7, 2 to the dereferenced pointers in your pointer array. Since they are uninitialized, they contain garbage and thus you're writing to random addresses in memory, which leads to the crash.

Delete characters form a string in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm beginning in C++ and I have a simple task. As title said, I want to delete first and last character from a string for x times (where x is the lenght of the string). For example, if the string is "example", the result will be:
example
xampl
amp
m
amp
xampl
example
So far, I'm thinking like this:
#include <iostream>
#include <string>
string sir = "Example";
int len, i;
len = sir.length();
for(i=1; i<=len; i++)
{
sir.erase(sir.begin(), sir.end());
cout<<sir;
}
Or something like that... Can someone help me ?
You want to delete both the first and last char.But in the example you also added them each step. It is not actually clear what you want. Whatever you want to delete or add the characters it is feasible to keep the string unchanged. So you should use substr. Check it out here.
The problem is that
you can not use std::string::erase with integral index such as int i,
you need to use std::string::iterator
but even if you use std::string::iterator, with the current logic you would be trying to increment an iterator after the erase has been called. (such iterator is invalid)
Possible solution: assign sir.begin() to your iterator after each erase.
Here's how it could look like:
std::string sir = "Example";
for(string::iterator i = sir.begin(); i != sir.end(); i = sir.begin())
{
sir.erase(i);
std::cout << sir << std::endl;
}
outputs:
xample
ample
mple
ple
le
e
Just note that after erasing characters from your std::string, these characters are lost. You can not "restore" them. For the other half, you'll have to come with more sophisticated approach, but I'll leave that to you :)