For this simple piece of code:
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::string;
bool are_uniq_chars(string const&);
int main(){
string s;
cout << "Enter string: ";
cin >> s;
auto flag = are_uniq_chars(s);
I get the following illegal value for s upon gdb debugging:
Thread 2 hit Breakpoint 1, main () at main.cpp:10
10 string s;
(gdb) n
11 cout << "Enter string: ";
(gdb) n
12 cin >> s;
(gdb) n
Enter string: vinod
13 auto flag = are_uniq_chars(s);
(gdb) p s
$1 = {<std::__1::__basic_string_common<true>> = {<No data fields>},
__r_ = {<std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>> = {<std::__1::allocator<char>> = {<No data fields>},
__first_ = {{__l = {__cap_ = 110429756552714, __size_ = 0,
__data_ = 0x0}, __s = {{__size_ = 10 '\n', __lx = 10 '\n'},
__data_ = "vinod", '\000' <repeats 17 times>}, __r = {__words = {
110429756552714, 0, 0}}}}}, <No data fields>},
static npos = 18446744073709551615}
Not sure what is wrong? The code is being run on macOS High Sierra 10.13.2.
Any help would be appreciated.
TIA
Vinod
Guess this output is normal as the program started working as expected after fixing another error.
Related
I'm new to coding in C and C++, and I have a program with
an issue. When I (try) to run it, it gives me this error:
"No suitable constructor exists to convert from "char" to "std::string".
I'm not sure what it means. My code is an example of a simple
substitution cipher covered in the book "Cracking Codes with Python" by Al Sweigart.
I just want to replicate it in C++. Here's my code:
#include <iostream> // for communicating with user
#include <string>
using namespace std;
string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // all symbols
string encrypt(string msg, string key, string mode) {
string ca = symbols;
string cb = key;
string translated;
if (mode == "decrypt") {
ca, cb = cb, ca;
}
int index = 0;
for (index = 0; index < msg.length(); index++) {
cout << "index is " << index << endl;
int sindex = ca.find(msg[index]); // it works here
cout << "sindex is " << sindex << endl;
string cl = cb[sindex]; // the problem
translated += cl;
}
return translated;
}
int main() {
string msg = "";
string key = "";
string mode = "";
string ciphertext = ""; // our variables
cout << "Enter message: (no spaces please)\n";
cin >> msg;
cout << "Enter key (or \"none\" for using default):\n";
cin >> key;
if (key == "none") {
key = "QWERTYUIOPASDFGHJKLZXCVBNM";
}
cout << "Enter mode: (\"encrypt\" or \"decrypt\")\n";
cin >> mode;
ciphertext = encrypt(msg, key, mode);
cout << "The ciphertext is\n" << ciphertext;
}
For some reason it works with msg on line 17 but not with cb on line 19, even though
they're both std::string. The actual error is on line 19 with string cl = cb[sindex];.
Not even sure what's wrong. It works on line 17 int sindex = ca.find(/*The thing here*/msg[index]);.
(Maybe my Visual Studio 2019 has gone nuts.) If I replace cb with msg it still gives me the
same error. Maybe line 17 is a lucky line? Who knows? But please help, I'm so
confused!
I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint.
(After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout", for example.
string convertDecToBin(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 2;
r = r / 2;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
string convertDecToOct(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 8;
r = r / 8;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
int main()
{
int input = 0;
while (input != -1)
{
cout << "\nEnter a decimal number (-1 to exit loop): ";
cin >> input;
if (input != -1)
{
cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
}
}
cout << "\n\nPress any key to exit. . .";
_getch();
return 0;
}
arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.
I am running this code:
*
ifstream trace_file;
trace_file.open (argv[7]);
while (!trace_file.eof())
{
string line;
getline (trace_file,line);
string read_write = line.substr(0,1);
string address = line.substr(2);
cout << line << '\t' << read_write << "-->" << address<< "-->" << line.size() << '\n';
}
*
The file that I am reading through ifstream, has this content:
r 4fd
w f47
r 8d2
w f05
I get this output:
r 4fd r-->4fd-->5
w f47 w-->f47-->5
r 8d2 r-->8d2-->5
w f05 w-->f05-->5
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Abort (core dumped)
It runs fine if I remove this line:
string address = line.substr(2);
Length of each string is 5, so this should not throw this exception.
It runs fine if I keep pos=0
for pos = 1 or 2 or 3, I get this exception.
I do not understand what is wrong here. Any help will be appreciated.
here is my code, I can not figure out why it won't work as a function when the exact code in main() produces the correct answer. The assignment is to convert binary number to decimal.
#include <iostream>
#include <cstdlib>
#include "std_lib_facilities.h"
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
enter code here`while (biNum > 0)
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
return biNum;
}
corrected code:
#include <iostream>
#include <cstdlib>
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
getchar();
return 0;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
return dec;
}
As you are not using { and } in your while loop may be its going in infinite loop. As its working for this line only
while (biNum > 0)
rem = biNum % 10; // running this line infinite as `biNum > 0`
Use
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
maybe this?
while (biNum > 0)
{
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
int main() or int main(int argc, char** argv) must return a value. If you return 0 then it means that there is no problem with code. Another numbers 1,2 etc means there is an error.(Returned numbers are error numbers)
I use R by rinside
std::string cmd = "m <-matrix(c(1:9),nrow=3))";
SEXP proxy = m_R.parseEvalNT(cmd);
Rcpp::NumericMatrix m(proxy);
the runtime to terminate it in an unusual way
anyone please help me
thanks
Odd. I played with this and also got some run-time errors; now they no longer arise. Try this:
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
//
// cf http://stackoverflow.com/questions/18403402/
//
// GPL'ed
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string cmd = "m <- matrix(c(1:9),nrow=3)";
Rcpp::NumericMatrix M = R.parseEval(cmd); // assign mat. M to NumericMatrix
int n = M.nrow();
int k = M.ncol();
std::cout << "All good:" << n << " by " << k << std::endl;
exit(0);
}
edd#max:~/svn/rinside/pkg/inst/examples/standard$ ./so20130823
All good:3 by 3
edd#max:~/svn/rinside/pkg/inst/examples/standard$
which I just copied into the examples/standard/ directory as a simple call to make will then build it.