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
the output of this program should be this:
Can anybody explain why the output of this main is:
F1/2 F2/3 F5/4 F0/1 F0/1 F0/1 F0/1 F0/1
K0/1 K0/1
K?/? K2/3 K1/2
can you explain how we get the last 2 lines ?
thanks
the constructor is initialize like this in fraction.h
Fraction(int n=0, int d=1);
/* fraction.cpp */
#include "fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction(int n, int d)
: numerateur(n)
{
dedominateur = d;
cout << "F" << n << '/' << d << ' ';
simplifier();
}
Fraction::~Fraction(){
//cout<<"destructeur";
cout << "K"
<< numerateur << '/'
<< dedominateur << ' ';
numerateur = dedominateur = 0;
}
void Fraction::simplifier(){/*...*/}
/* prog1.cpp */
#include <iostream>
#include "fraction.h"
using namespace std;
void test(Fraction a, Fraction& b){
Fraction* c = new Fraction(a);
a = b;
b = *c;
c = NULL;
cout<< "F";
return;
}
int main(){
Fraction f1(1,2), f2(2,3), f3(5,4);
Fraction* tab = new Fraction[5];
std::cout << std::endl;
test(f1, tab[2]);
test(tab[3], tab[4]);
f3 = tab[5];
std::cout << std::endl;
return 0;
}
The output with a K obviously comes from the destructor. Two of them would be from the parameter a which is passed by value (copy) to test. The other three are the ones on the first line of main.
The objects created by new are never delete'd, so they are never destructed.
Related
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
#include <iostream>
using namespace std;
int main()
{
// Local variable declaration:
int a = 10;
// while loop execution
while (a < 20)
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// Local variable declaration:
int a = 100;
// while loop execution
while (a > 0)
{
cout << "value of a: " << a << endl;
a--;
}
return 0;
}
Strange indentation. (Now fixed)
But a while loop is based on a condition which means you should initialise the variable a to be 100.
After this use a loop to check while a is greater than 0. Then in the loops body you can output the variable a and decrement the number.
#include <iostream>
using namespace std;
int main()
{
int a = 100;
while (a > 0) {
cout << "value of a: " << a << endl;
a--; // Decrement A
}
}
Another method would be use a for loop
#include <iostream>
using namespace std;
int main()
{
for (int a = 100; a > 0; a--) {
cout << "value of a: " << a << endl;
}
}
These are very simple ways and I'd recommend looking into some books for beginners if you are new to C++ to understand the different syntax of loops.
You can modify your loop like this:
#include<iostream>
#include<ranges>
namespace sv = std::views;
int main()
{
for (int i : sv::iota(1, 101) | sv::reverse)
std::cout << i << "\n";
}
Here's a demo.
Note that this code is only valid from C++20.
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 5 years ago.
Improve this question
I'm trying to center text in a function, but define the function in a header file called center.h
center.h:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void centerStr(string* str) {
int consoleWidth = 80;
cout << setw(consoleWidth / 2) << " " << str << endl;
}
main.cpp:
#include <iostream>
#include <iomanip>
#include "center.h"
using namespace std;
int main() {
system("clear");
cout << centerStr("Unit Converter By DualKeys") << endl <<
endl;
return 0;
}
In main.cpp I keep getting an error saying "No matching function for call to centerStr"
[EDIT] Yes, I have tried defining centerStr in the main.cpp file
This seems to me to be an incredibly messy way of setting things up but anyway.
I would avoid putting...
using namespace std;
in any header files or at all to keep the code clean.
center.h
//declaration
void centerStr(const char*);
center.CPP
#include "center.h"
#include <iomanip>
#include <iostream>
//definition
void centerStr(const char* str) {
int consoleWidth = 80;
std::cout << std::setw(consoleWidth / 2) << " " << str << std::endl;
}
main.cpp
#include "center.h"
int main() {
centerStr("Unit Converter By DualKeys");
system("PAUSE");
return 0;
}
You will need an overload for an std::string version of this function or a function template may suffice.
template<typename T>
void centerStr(const T& t) {
int consoleWidth = 80;
std::cout << std::setw(consoleWidth / 2) << " " << t << std::endl;
}
Finally just declare consoleWidth as a global const variable. Seems wasteful to do it on each call! :)
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 6 years ago.
Improve this question
Please help I'm beginner level student in C++
I'm failed to find a proper solution.I also added error image in this question.Please give me answer with proper solution.
#include <iostream>
#include <conio.h>
class test
{
int no;
static int count;
public:
void getval (int);
void dispcount (void);
};
void test:: getval(int x)
{
no = x;
cout << "Number = " << no << endl;;
count++;
}
void test::dispcount(void)
{
cout << "Counten = " << count;
}
int test::count;
int main()
{
test t1,t2,t3;
t1.dispcount();
t2.dispcount();
t3.dispcount();
t1.getval(100);
t2.getval(200);
t3.getval(300);
t1.dispcount();
t2.dispcount();
t3.dispcount();
getch();
return 0;
}
here is error.jpg
Include directive
#include <iostream>
#include <conio.h>
using namespace std;
//..
Or include using declarations like
#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;
//...
Or use qualified names as for example
void test:: getval(int x)
{
no = x;
std::cout << "Number = " << no << std::endl;
^^^^^^^^^ ^^^^^^^^^^
count++;
}
Identifiers cout and endl are declared in namespace std and not in the global namespace.
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
I am trying to create my own hash class. However after countless tries I cannot get my program to work correctly. I believe that there is an error in me calling the function, however I am not quite sure. Can anyone help me figure out what I am doing wrong and possibly show me how to fix it?
hash.h
#include <iostream>
class MyHash {
public:
MyHash();
int hashCode(char, char);
};
hash.cpp
#include <iostream>
#include "Hash.h"
MyHash::MyHash() {}
int MyHash::hashCode(char first_letter, char last_letter) {
int hash = 0;
int ascii = 1;
hash = ((ascii * first_letter) + (ascii * last_letter)) % 23;
return (hash);
}
driver.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "Hash.h"
using namespace std;
int main() {
vector<string> words;
int first_letter;
int last_letter;
string word;
int n = 0;
int hash = 0;
for (int i = 0; i < 5; i++) {
cout << " Please enter a word: ";
cin >> word;
words.push_back(word);
}
vector<string>::iterator itr = words.begin();
for (; itr != words.end(); itr++) {
first_letter = *itr[n].begin();
last_letter = *itr[n].rbegin();
cout << endl << " " << first_letter << " " << last_letter << endl;
hash = hashCode(first_letter, last_letter) cout << hash << endl;
}
return 0;
}
Why are you wrapping your function inside a class? It's completely arbitrary to define a class, place no data whatsoever inside of it then try to call the function from it without declaring any objects.
int hashCode(char first_letter, char last_letter) {
int hash = 0;
int ascii = 1;
hash = ((ascii * first_letter) + (ascii * last_letter)) % 23;
return (hash);
}
If you wanted to use a class, you need to have a structure along the lines of:
class myHash{
public:
myHash();
insert();
remove();
private:
std::vector<std::string> words;
hash();
rehash();
};
So to get over the compilation problem and not change the overall structure of the program you'll need to change your call to hashDode(...) to be MyHash::hashCode(...) and also change your declaration of int hashCode(char, char); to be static int hashCode(char, char);.
Youcan't just call a function defined in some scope and expect the compiler to figure it out, you need to give some indication as to where the function is. Since it's a class method you need to specify a class object or the class itself.
The static keyword will allow you to call the function without an object, which is OK in this case since you don't have any data in your object.
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 usually try not to ask questions, but this has had me stumped for a while. So my question is how can I check the value of string HomeWTD in a "if Statement" in the following code?
Main.cpp:
#include "Header.h"
using namespace Header;
void main()
{
Home();
if (NEED HELP HERE)
{
}
}
Header.h:
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
//Create A Namespace called "Header"
namespace Header
{
using namespace std;
}
//Functions
string Home()
{
string HomeWTD;
string LoginTxt = "Login";
string RegisterTxt = "Register";
string OptionsTxt = "Options";
string CreditsTxt = "Credits";
string QuitTxt = "Quit";
string HomeHeaderMsg = " Home ";
cout << HomeHeaderMsg;
cout << "----------" << endl;
cout << LoginTxt << endl;
cout << RegisterTxt << endl;
cout << OptionsTxt << endl;
cout << CreditsTxt << endl;
cout << QuitTxt << endl << endl;
getline(cin, HomeWTD);
return HomeWTD;
}
void Register()
{
string UsernameIn;
string PasswordIn;
string UsernameOut;
string PasswordOut;
getline(cin, UsernameIn);
getline(cin, PasswordIn);
ofstream UserFile;
UserFile.open(UsernameIn + ".UserSav");
UserFile << PasswordIn;
};
Either save the result and compare later:
string result = Home();
if( result == "foo" ) {
...
}
Or use the result inline:
if( Home() == "foo" ) {
...
}
#include "Header.h"
using namespace Header;
void main()
{
string answer = Home();
if (answer == "something")
{
...
}
}
Using Home() is like just using the value of HomeWTD.