I dont get the correct ouput [closed] - c++

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 4 years ago.
Improve this question
I'm trying to copy an specific word from a C array and put it into another c array so then later I can show the output, however when I execute the program the first function call works, (I get sprightful in the output/terminal)
"Copy \"sprightful\", should see \"sprightful\".\n";
however, when I call the function again it gives me this result (output/terminal) superhtful instead of super (since I specified in the argument, that I want to copy the first five characters). what's wrong?
#include <iostream>
#include <cstring>
#include <cctype>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
// function declarations
char* copy(char* destination, const char* source, size_t num);
int main()
{
const int WORDSIZE = 15;
char words[][WORDSIZE] = {"sprightful", "reason to row", "New York", "Bolton", "Frida", ""};
char word[WORDSIZE];
copy(word, words[0], sizeof(word) - 1);
cout << word << endl << endl;
copy(word, "Supercalifragilisticexpialidocious", 5);
cout << word << endl << endl;
return 0;
}
char* copy(char* destination, const char* source, size_t num)
{
strncpy(destination, source, num);
return destination;
}

You aren't clearing word in between the two function calls. On the second call it only overwrites the first 5 characters ("super"), and the rest is the result of your previous call.

#Brandon is correct. Try clearing word between your function calls:
int main()
{
const int WORDSIZE = 15;
char words[][WORDSIZE] = { "sprightful", "reason to row", "New York", "Bolton", "Frida", "" };
char word[WORDSIZE];
// Test the copy function
std::cout << "Copy \"sprightful\", should see \"sprightful\".\n";
copy(word, words[0], sizeof(word) - 1);
cout << word << endl << endl;
memset(&word[0], 0, sizeof(word)); //Clear the char array.
// Test the limit on the copy function
cout << "Copy \"Supercalifragilisticexpialidocious\", should see \"Super\".\n";
copy(word, "Supercalifragilisticexpialidocious", 5);
cout << word << endl << endl;
getchar();
return 0;
}

Related

I am getting an [expected unqualified-id] error on the second to last curly bracket [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 1 year ago.
Improve this question
#include <iostream>
#include <cmath>
//This program will calculate the area of a circle and the volume of the sphere using a given
radius value//
using namespace std;
void multivalues(int & , int & , int &, int&) ;
int main()
{
int value, squared, cubed, fourth;
value = 2;
multivalues(value,squared,cubed,fourth);
cout << "The value of the squared is " << squared << endl;
cout << "The value of the cubed is " << cubed << endl;
cout << "The value of the fourth is " << fourth << endl;
return 0;
}
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
This is for an assignment leading to understanding passing by reference. This program should calculate the area of a circle and the volume of the sphere using a given value. It doesn't compile however.
You have a ; where you don't want one:
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
Look at the end of the first line I included.

I got a memory error and i dont know whats cauising it in c++ [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
Hello this code is part of a big code and it causing a lot of problem i dont know why. i tried to use structer and vectors its ggot an error about vector out of range then i switched to class but still got same error im now compiling just problematic and its looks like a memory error. how can i fix this ?
#include <time.h>
#include <locale.h>
#include <vector>
#include <exception>
using namespace std;
class işlem {
string işlemdetay; int işlemtutar; time_t işlemtarih; int hesapno;
public:
vector <işlem> işlemler;
işlem() { işlemdetay = " "; işlemtutar = 0; işlemtarih = time_t(0); hesapno = 0; };
işlem(string İşlemdetay, int İşlemtutar, int Hesapno) {
işlemdetay = İşlemdetay; işlemtutar = İşlemtutar; hesapno = Hesapno;
işlem newişlem(işlemdetay, işlemtutar, hesapno);
işlemler.push_back(newişlem);
}
void listele(int hesapid) {
int size = işlemler.size();
for (; size >= 0; size--)
{
if (işlemler[size].hesapno == hesapno)
{
//cout << "İşlem Tarihi: " << işlemler[size].işlemtarih << endl;
cout << "İşlem Tutar: " << işlemler[size].işlemtutar << endl;
cout << "İşlem Detayı: " << işlemler[size].işlemdetay << endl;
}
}
}
};
int main()
{
try
{
işlem newişlem("Hesap oluşturuldu.", 400, 1);
}
catch (const std::exception e)
{
cout << e.what();
}
}```
işlemler[işlemler.size()] is out-of-range. The initial value of size should be işlemler.size() - 1, not işlemler.size().

Saving A Character that you pop out of a char vector [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 2 years ago.
Improve this question
Im trying to save a char that im going to pop out of the char vector can you do that? If so how?
You are saving the character. This code
char char_toInt = vect[vect.size() - 1];
saves the character perfectly well. Not sure why you thought that was incorrect.
The mistake is the next line
int new_int = stoi(char_toInt);
Instead do it like this
int new_int = char_toInt - '0';
stoi is for a string and you have a char. These are not the same thing, and C++ is fussy about types.
Instead to convert a digit char to it's integer value just subtract '0' from the char. All chars are represented as integers internally (lookup ASCII if you are interested in the how this might be done). Subtracting '0' gives you the difference between the integer corresponding to your digit and the integer corresponding to '0'. Since the integers corresponding to the digits are guaranteed to be sequential this gives you the value of your digit.
Well, you can write something like this
#include <vector>
#include <iostream>
int main()
{
std::vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
std::cout << v.back() << std::endl;
v.pop_back();
std::cout << v.back() << std::endl;
v.pop_back();
std::cout << v.back() << std::endl;
v.pop_back();
return 0;
}
which would produce an output of
c
b
a
Alternatively, you can also use a queue:
#include <queue>
#include <iostream>
int main()
{
std::queue<char> q;
q.push('a');
q.push('b');
q.push('c');
std::cout << q.front() << std::endl;
q.pop();
std::cout << q.front() << std::endl;
q.pop();
std::cout << q.front() << std::endl;
q.pop();
return 0;
}
which would produce an output of:
a
b
c
Or, alternatively, you can use a stack:
#include <stack>
#include <iostream>
int main()
{
std::stack<char> s;
s.push('a');
s.push('b');
s.push('c');
std::cout << s.top() << std::endl;
s.pop();
std::cout << s.top() << std::endl;
s.pop();
std::cout << s.top() << std::endl;
s.pop();
return 0;
}
which would produce an output of:
c
b
a

Run Time Check Failure #2 S 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 6 years ago.
Improve this question
I'm having trouble on a C++ program. It keeps giving me a Run Time Error # 2 - S. It builds fine though. It builds fine. Main is supposed to call two other functions, one which puts ints into an array, the other which reverses the numbers' positions on the arrays, essentially creating a 'reverse' array.
#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;
void storeArray(int[], int);
void flipArray(int[], int);
int main()
{
const int arraysize = 10;
int foo[arraysize]; // everyone seems to use foo so I thought I'd try it
storeArray(&foo[arraysize], arraysize);
cout << endl;
flipArray(&foo[arraysize], arraysize);
return 0;
}
void storeArray(int foo[], int arraysize)
{
int counter;
counter = 0;
while (counter < arraysize)
{
cout << "Enter number " << (counter+1) << " : ";
cin >> foo[counter]; counter++;
}
return;
}
void flipArray(int foo[], int arraysize)
{
int counter2;
int counter3;
counter2 = 0;
counter3 = 9;
for (counter2; counter2 <= 9; counter2++)
{
cout << "Value number " << (counter2 + 1) << " is " << foo[counter3] << endl;
counter3--;
}
cout << endl;
return;
}
Any help would be appreciated. Thanks!
storeArray(&foo[arraysize], arraysize);
This passes the address just past the end of foo
Instead use:
storeArray(foo, arraysize);
And:
void storeArray(int* foo, int arraysize)
Likewise for:
flipArray(&foo[arraysize], arraysize);
EDIT: just noticed you're #include <array> and using C-style arrays. Not the C++ STL arrays defined in #include <array>

How to use an if statement in a string 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 7 years ago.
Improve this question
The following code is supposed to randomly pick a string from the array and then say "Yay!" if Happy has been chosen.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int main()
{
srand(time(NULL));
string textArray[4] = { "Happy", "Sad", "Mad", "Overjoyed." };
int RandIndex = rand() % 4;
cout << textArray[RandIndex] << endl;
//if (textArray == "Happy") old
if (RandIndex == 0) //new
{
cout << "Yay!" << endl;
}
cin.get();
}
My problem is that the operand types are incompatible with strings and chars. What would be the best solution to this problem?
EDIT: All I needed to do was replace "if (textArray == "Happy")" with "if (RandIndex == 0)"
For example like
if ( textArray[RandIndex] == "Happy" )
{
cout << "Yay!" << endl;
}
or like
if ( RandIndex == 0 )
{
cout << "Yay!" << endl;
}
Also it would be better to write at least like
string textArray[] = { "Happy", "Sad", "Mad", "Overjoyed." };
const size_t N = sizeof( textArray ) / sizeof( *textArray );
size_t randIndex = rand() % N;