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
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.
I'm trying to take a Magic 8 Ball program that was originally using arrays and change it to a program that uses vectors instead. The task that I was given was to take the code below and do a couple of things to it.
use the push_back() function to initialize the vector
modify the signature and prototype of the getAnswer() function
modify the code in the body of the getAnswer() function
remove any unneeded code, such as your constant for the number of
answers
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <fstream>
#include <stdio.h>
using namespace std;
string getAnswer();
const string exitString = "x";
const int SIZEOF_ANSWERS = 8;
string magicEightBallAnswers[SIZEOF_ANSWERS] = { "Yes", "No", "Maybe", "It's not certain", "The outlook is good",
"The outlook is poor", "Time will tell", "Most likely" };
int main(int argc, char *argv[])
{
bool keepGoing = true;
while (keepGoing)
{
string question;
//prompt for and get the question
cout << "What is your question? (Enter 'x' to exit)" << endl;
getline(cin, question);
//this assumes that the user enters a lower case x
if (question.compare(exitString) == 0)
keepGoing = false;
else
{
cout << getAnswer() << endl;
}
}
return 0;
}
string getAnswer()
{
int index = rand() % SIZEOF_ANSWERS;
return magicEightBallAnswers[index];
}
This example might help:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
string getAnswer(vector<string> & magicEightBallAnswers)
{
int i = rand() % magicEightBallAnswers.size();
return magicEightBallAnswers[i];
}
int main()
{
vector<string> magicEightBallAnswers {
"Yes",
"No",
"Maybe",
"It's not certain",
"The outlook is good",
"The outlook is poor",
"Time will tell",
"Most likely"
};
// Initialize rand()
srand(time(NULL));
string question;
while (true) {
// Prompt for and get the question
cout << "What is your question? (Enter 'x' to exit)" << endl;
getline(cin, question);
if (question == "x")
break;
// Ask question
cout << getAnswer(magicEightBallAnswers) << endl;
}
// Done
cout << "Bye! Let's play again soon!" << endl;
return 0;
}
Specifically:
Use C++ features to your advantage, to eliminate unnecessary code like "push_back() or initializing with "(8)".
Never use a hard-coded constant like "SIZEOF_ANSWER" if there's a dynamic alternative like "vector.size()".
Note the use of pass by reference: in string getAnswer(vector<string> & magicEightBallAnswers).
You should call "srand()" with a seed before using "rand()".
Etc.
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.