What is wrong with this simple C++ class program? [closed] - c++

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.

Related

How do I modify this loop to list numbers 100 to 1 [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 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.

c++ What's the output of this function? [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
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.

Is it possible to make a header file to center text? - In Console [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 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! :)

take time between two inputs c++ [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 8 years ago.
Improve this question
Im having tropel with a little project of mine and would like some help.
this is the code so far.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(timetaker)
{
}
int after()
{
srand(data from above);
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
}
what im having trople with is the function that takes time and gives it to the int after() function. But i would be gratefull for some help with the int main (timetaker)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
std::cout << "First Click: ";
std::cin.ignore();
unsigned int start = clock();
std::cin.ignore();
std::cout << "Next when you are ready ";
std::cin.ignore();
std::cout << "Time taken in millisecs: " << clock() << endl;
std::cout << "Now for the random number. Are you ready" << endl;
std::cin.ignore();
srand(clock());
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
std::cout << "That is the random number from the time taken.";
return 0;
}
It's easier if you put the code you want to call before the place you want to call it from. Getting a time value for srand(), and passing it from main(), can be done as illustrated below...
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void after(time_t seed)
{
srand(seed);
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
}
int main()
{
do_stuff(time(NULL));
}
Considering the very vague nature of this question, I'd say this is your best resource:
http://www.cplusplus.com/reference/ctime/time/
I don't see any question. By the way, you're missing the return value for main and after. Also you can't do int main (timetaker) what should that be ?
Your srand function should work with data from the main function ? You will need to pass some parameters in your function int after.
I also would not recommend using using namespace std; as this may cause undefined behavior if you're going to implement, for instance, your own cout function. Using std:: is the better way. Still, it's your choice, and in this code it's fine.
If you want to work with time, you may check out those links:
Time
Clock

How can I get the return value of function 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 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.