Hi guys i found a timer source code for c++
Found it here:
https://www.daniweb.com/programming/software-development/code/216933/a-countdown-timer-in-c
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
int main()
{
int m, s, h;
cout << "A COUNTDOWN TIMER " << endl;
cout << "enter time in hours here" << endl;
cin >> h;
cout << "enter time in minutes here " << endl;
cin >> m;
cout << "enter im in seconds here" << endl;
cin >> s;
cout << "Press any key to start" << endl;
cout << " A COUNTDOWN TIMER" << endl;
cout << "time remaining" << endl;
cout << "hours : " << h << "mins : " << m << " secs : " << s << endl;
for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0; min--)
{
if (min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if (sec == 0)
s = 59;
Sleep(1000);
system("cls");
cout << hour << " :hours " << min << " :mins " << sec << " :secs" << endl;
}
}
}
Sleep(1000);
cout << "THE END" << endl;
return 0;
The problem i keep getting is:
Severity Code Description Project File Line Suppression State
Error C1075 the left brace '{' was unmatched at the end of the file
I'm not sure where I'm supposed to add a } ?
Thanks in advance!
Looks like you're missing your end brace at the end of your main function after the return 0;
Related
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 4 years ago.
Improve this question
I haven't got to know a lot about functions yet since I'm just at chapter 4 (which is decision making using if, else) of Starting Out with C++ book.
Here my problem asks me to enter the inputs of runner names and their timer so I could rank them in 1st 2nd and 3rd place.
I'm having trouble to validate the input for the string for runners's names as letters and spaces only.
I'm thinking about using loop or bool but I don't know what is the proper way to put it.
Can somebody show me what function I can use best in this case without repeating step into multiple lines of codes.
I'm sorry in advance for my long version code, I'm also looking for the way to shorten it.
#include <iostream>
#include <string>
#include <iomanip> // for setw, setpresicion, control output
#include <cctype> // for getline, cin.
using namespace std;
int main()
{
string runner1, runner2, runner3;
double time1, time2, time3;
cout << "Enter Runner 1 name: ";
getline(cin, runner1);
cout << "Enter Runner 2 name: ";
getline(cin, runner2);
cout << "Enter Runner 3 name: ";
getline(cin, runner3);
cout << runner1 << "'s finishing time: ";
cin >> time1;
while (!cin || (time1 < 0)){ //the # is negative
cout << "Please enter a non-negative number!\n";
cin >> time1;
}
cout << runner2 << "'s finishing time: ";
cin >> time2;
while (!cin || (time2 < 0)){ //the # is negative
cout << "Please enter a non-negative number!\n";
cin >> time2;
}
cout << runner3 << "'s finishing time: ";
cin >> time3;
while (!cin || (time3 < 0)){
cout << "Please enter a non-negative number!\n";
cin >> time3;
}
// This is for Rank1----------------------------
cout << "1st place : ";
if((time1 < time2)&&(time1 < time3))
{
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if (time2 < time3){
cout << left << setw(5) << runner2 << " " << right
<< setw(5) << time2 << endl;
}
else {
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
//Rank2------------------------------------
cout << "2nd place : ";
if ((time1 < time2)&&(time3 < time1)){
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if ((time3 < time2)&&(time1 < time3)){
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
else {
cout << left << setw(5) << runner2 << " " << right
<<setw(5) << time2 << endl;
}
// RANK 3-----------------------------------
cout << "3rd place : ";
if ((time1 > time2)&&(time1 > time3)){
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if ((time1 < time3)&&(time3 > time2)){
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
else {
cout << left << setw(5) << runner2 << " " << right
<<setw(5) << time2 << endl;
}
return 0;
}
This routine will do the validation:
bool validateString(const std::string& s)
{
for (const char c : s) {
if (!isalpha(c) && !isspace(c))
return false;
}
return true;
}
You might want to use something like
#include <cctype>
#include <string>
#include <iostream>
int main()
{
std::string foo;
bool valid;
do {
std::getline(std::cin, foo);
valid = true;
for (std::size_t i{}; i < foo.length() && valid; ++i) {
if (!(std::isalpha(static_cast<unsigned char>(foo[i])) ||
std::isspace(static_cast<unsigned char>(foo[i]))))
valid = false;
}
} while (!valid);
}
which checks every character for being a letter or a space.
Here is my proposal for a solution:
#include <iostream>
#include <string>
bool validateString(std::string toCheck) {
bool correct = false;
for (int i = 0; i<toCheck.length(); i++) {
if ((toCheck[i] >= 65 && toCheck[i] <= 90) || (toCheck[i] >= 97 && toCheck[i] <= 122) || toCheck[i] == 32) {
correct = true;
}
else {
return false;
}
}
return correct;
}
int main()
{
std::cout << validateString("Whats up peeps") << std::endl;
std::cout << validateString("234235") << std::endl;
system("pause");
return 0;
}
Using ASCII Table.
Note: Might not be the best way to do it but it works.
I can't figure out how to make my code go to the next else if statement if my user input satisfies the previous if state.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
in your code if input > 60 it will satisfy condition and will not execute else part so first check wether input > 86400 if not then check if input > 36000 if not then check for input > 60
try below code in which if conditions are reversed
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
return 0;
}
Make it other way round.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else
{
cout << "The time is " << input << " seconds." << endl;
}
return 0;
}
Hope this is what you want!
There are multiple ways to do this, you can implement a check between 2 values using conditions like >= val1 and < val2 or ensure that the order in which the checks happen is different.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
or another way would be to change the order, once the particular if statement is valid, it doesn't check with the rest of the if statements.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input < 60)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
Your if else conditions are not completely defined... you have to set those in a range... otherwise is not going to work...
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600))
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400))
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
My question is this, how do I save an int value in a while loop, my code is all about gambling, you start with 1,000 and you want to make the most amount of cash, but when I roll again my cash restores back to its original value that I set.
My code is this (Note I am new so do not laugh at how bad it is)
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
char again = 'Y';
int test;
int yes;
int CashW;
CashW = 1000;
int CashL;
CashL = 1000;
int yLose;
yLose = 500;
int xCash;
xCash = 1000;
int xRan;
srand(time(0));
xRan = rand() % 100 + 1;
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you lose!" << endl;
while (again == 'y' || again == 'Y')
{
cout << " The Number I Choose Is: " << xRan << endl;
CashL = xCash - xCash - xCash;
CashW = xCash + xCash;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashW << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashL << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (yes == 1) {
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
}
}
In your code you currently display either CashW or CashL depending on the gampbling result.
Unfortunately, you only print out the result and never store it into xCash. So at next iteration you start again with the same xCash value !
You can easily solve this by adding xCash = CashW; or xCash = CashL; just under the line in which you display the result.
You are never updating xCash with the amount of each win/loss. You are not generating a new random number on each loop iteration. And you are stuck in an endless loop because you never update the loop variable again.
Try something more like this instead:
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int CashW = 1000;
const int CashL = 1000;
int xCash = 1000;
int xRan;
char answer;
srand(time(0));
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you win!" << endl;
do
{
xRan = rand() % 100 + 1;
cout << " The Number I Choose Is: " << xRan << endl;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash += CashW;
cout << " The cash you have now is: " << xCash << endl;
}
else if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash -= CashL;
cout << " The cash you have now is: " << xCash << endl;
}
else {
cout << " dang, a draw!" << endl;
}
cout << " play again? " << endl;
cin >> answer;
if ((answer != 'y') && (answer != 'Y')) {
cout << " All done? Come back again another time!" << endl;
break;
}
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
while (true);
return 0;
}
I am having an issue learning C++ basics involving the while loop. When
I run my program it works well, until I get to the while loop involving an OR (||) statement. while (totalhumanHP > 0 || totalskeletonHP > 0) should continue until one hits zero then stop correct?
It is only keeping tabs on totalhumanHP and letting skeletonHP dip into the negatives without stopping. I am not sure how or why the OR statement isnt functioning in the while loop.
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
cout << "-----SKELETMANS V HOOMINS-----\n";
int skeletonHP = 90;
int humanHP = 100;
int skeletonDMG = 1;
int humanDMG = 1;
int totalskeleton = 1; //Inputted Skele's
int totalhuman = 1; //Inputted hoomins
int totalskeletonHP; //Overall Skele HP
int totalhumanHP; //Overall Hoomin HP
string battlecry;
string battlecryenter;
default_random_engine generator(time(0));
uniform_int_distribution<int> skeletonrng(1, 22);
uniform_int_distribution<int> humanrng(1, 20);
cout << "How Many Skeletmans: ";
cin >> totalskeleton;
cout << "How Many Houmints: ";
cin >> totalhuman;
totalskeletonHP = totalskeleton * skeletonHP;
totalhumanHP = totalhuman * humanHP;
cout << "Overall Skeleton HP= " << totalskeletonHP << endl;
cout << "Overall Hoomint HP= " << totalhumanHP << endl;
cout << endl;
cout << "What is your battlecry?\n (You can use this to start the battle)";
cout << endl;
cin >> battlecry;
battlecry == battlecryenter;
cout << "Use Your Battlecry To Start The Fight!!!\n\n\n\n\n";
cout << "BattleCry: ";
cin >> battlecryenter;
if (battlecryenter == battlecry)
{
cout << "THE FIGHT HAS COMMENCED!!!\n\n\n\n";
}
else{
cout << "You 4got alreedy? UR DOMB MING\n\n";
cout << "ACTUAL BattleCry: ";
cin >> battlecryenter;
if (battlecryenter == battlecry)
{
cout << "THE FIGHT HAS COMMENCED!!!\n\n\n\n";
}
}
int endskeleton = 0;
int endhuman = 0;
while (totalhumanHP > 0 || totalskeleton > 0)
{
skeletonDMG = skeletonrng(generator);
humanDMG = humanrng(generator);
totalhumanHP = totalhumanHP - skeletonDMG;
totalskeletonHP = totalskeletonHP - humanDMG;
cout << "END SKELETON: " << totalskeletonHP << endl;
cout << "END HOOMING: " << totalhumanHP << endl;
}
cout << "Skeleton Ramaining Health: " << totalskeletonHP << endl;
cout << "Human Remaining Health: " << totalhumanHP << endl;
system("PAUSE");
return 0;
}
That's because you used OR, so even if skeletonHP is negative, that condition is still true since totalhumanHP is greater than 0.
Just change it with:
while (totalhumanHP > 0 && skeletonHP > 0)
When either one reaches 0, the loop will stop.
I've been playing around with some code in my down-time from my degree and I've nested a do{}while() loop inside another one but the problem I'm having is that the code keeps going until the last van is full, even after the number of parcels has been fulfilled...
The code's below. If someone could take a look at it and tell me what I've done wrong that'd be awesome. Bare in mind I've only really been coding in C++ for about a month so I've still got hella lot to learn..
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while ((fCurrentPayload + fParcelWeight)) < fVanCapacity)
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCout;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
Change this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity);
to this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity
&& iParcelLoaded < iParcelCount);
That way you will load as many items the user inputs. You code contains many syntax errors.
I corrected them for you, but please be more careful next time you post.
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestVanWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount);
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCount;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}