Very new to programming and l can't find any basic explanation online or code that will work well for what l need. I have a fairly long piece of programme (approx 300 lines) it all works. This is the structure to give an idea:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//code....
{
//code... etc...
}
}
I want to ask the user to repeat the programme. If enters y, then repeat int main up to the point of asking the same repeat question again. Else to cout<< "e.g. Thank you, goodbye";
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
//using namespace std; <--- Don't use using namespace std, it pollutes the namespace
void repeat()
{
//... code to repeat
}
int main()
{
//code....
char answer;
while((std::cin >> answer) != 'y')
{
repeat();
}
}
#include <iostream>
#include <conio.h>
using namespace std;
//Class
class DollarToRs {
public:
int Dollar;
int Rs;
int ToRs;
ConversionToRs() {
cout << "Enter the amount of Dollar: ";
cin >> Dollar;
ToRs = Dollar * 154;
cout << "This is the total amount in PKR: " << ToRs <<endl;
}
};
int main()
{
//Dollar Convertion Function
DollarToRs convert;
convert.ConversionToRs();
//For Repeating Program
int repeat;
int exit;
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
while (repeat == 1) {
convert.ConversionToRs();
cout << "To repeat program enter 1" <<endl;
cin >> repeat;
}
exit =0;
if (exit == 0) {
}
getch();
return 0;
}
Here's an example of a simple solution:
int main()
{
for (;;) // "infinite" loop (while (true) is also possible)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
if (answer == 'n')
break; // exit loop
} // else repeat
cout << "Thank you, goodbye" << endl;
}
Here's another one:
int main()
{
bool repeat = true;
while (repeat)
{
// stuff to be repeated here
cout << "Repeat? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you, goodbye" << endl;
}
As a side note, don't do this: #include <stdlib.h>. In C++ you should use the c prefixed header file names when using the C headers: #include <cstdlib> and #include <ctime>.
Related
I have the following code, this can check if the input is an integer; however, if something like '5o' is input it still flows through, can someone help me make sure all the digits input into x are correct, thank you!
#include <iostream>
using namespace std;
int main() {
cout << "enter x please: ";
int x;
cin >> x;
while (cin.fail()) {
cout << "sorry wrong input, try again: ";
cin.clear();
cin.ignore();
cin >> x;
}
cout << "correct input!";
return 0;
}
Read an entire line at once as a string and validate:
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cctype>
int main()
{
std::printf("Enter x: ");
std::string line;
while (true)
{
if (!std::getline(std::cin, line))
{
std::puts("Stream failed."); // Not much we can do
return -1;
}
if (std::all_of(line.cbegin(), line.cend(), std::isdigit))
break;
else
std::printf("Input is not a number. Try again: ");
}
int const x = std::stoi(line);
std::printf("x = %d\n", x);
}
//This is my code so far
//It is in microsoft studio visual
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
int main()
{
int a, b;
cout << "Input a whole number:"; //The following lines are to make it interactive
cin >> a;
cout << "Input a whole number:";
cin >> b;
int result = 1;
for (int i = 0; i < b; i++)
result *= a;
cout << "The Answer is:" << result;
cout << "\n";
system("PAUSE");
}
//How do I expand the power of it to handle more?
I just need to increase the power of it to handle more. I need this for a class and I can't quite figure it out. I met the qualifications, but I want it to be more powerful.
This is what I have so far. I have been testing it on c++ shell and it gives me error.
#include <iostream>
#include <string>
int main() {
cout << "Enter a number between 3 and 12: ";
int n;
cin >> n;
if(n>=3 && n<=12)
cout<<"Good number."endl;
else
cout<<"Bad number"endl;
return 0; //indicates success
}//end of main
cout,cinand endl are part of the standard library so you need to use std::cout, std::cin and std::endl. You can use using namespace std; on the beginning as well (after the includes, but it is considered as bad programming style).
Change your output to:
std::cout << "Good number." << std::endl;
Here is a working example:
int main() {
std::cout << "Enter a number between 3 and 12: ";
int n;
std::cin >> n;
if(n>=3 && n<=12)
std::cout<<"Good number."<<std::endl;
else
std::cout<<"Bad number"<<std::endl;
return 0; //indicates success
}
Below code is able to send a single student information to a file at once. How can it modified to send more records one after another without exiting and re-opening the program. I'm new for this. Kindly help.
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <windows.h>
#include <sstream>
#include <algorithm>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
#include <dos.h>
using namespace std;
string userName;
string passWord;
string selection;
int option;
struct patientinfo {
string PatientFname;
string PatientLname;
int Age;
int ContactNo;
string TreatmentType;
string AppDate;
string AppTime;
int eReciptId;
};
int num;
patientinfo emp[50];
void makeBooking()
{
ofstream outputFile;
outputFile.open("smt.bin", std::ofstream::in | std::ofstream::out | std::ofstream::app);
int i=num;
num+=1;
cout<< endl << endl << endl << endl << endl << endl
<< setw(30)<<"First Name : ";
cin>>emp[i].PatientFname;
outputFile <<emp[i].PatientFname <<",";
cout<< setw(30)<<"Last Name : ";
cin>>emp[i].PatientLname;
outputFile <<emp[i].PatientLname <<",";
cout<< setw(30)<<"Age : ";
cin>>emp[i].Age;
outputFile <<emp[i].Age <<",";
}
int main ()
{
makeBooking();
return 0;
}
Considering you have 50 students to whom you want to send information you should call the makeBooking function 50 times. So changing your main in
int main ()
{
for (int i = 0; i < 50; i++) {
makeBooking();
}
return 0;
}
should do the trick.
However, a more elegant solution would be to send the index i as a parameter in your function. So the code would be:
patientinfo emp[50];
void makeBooking(int i)
{
ofstream outputFile;
outputFile.open("smt.bin", std::ofstream::in | std::ofstream::out | std::ofstream::app);
// int i=num; you don't need these anymore
// num+=1;
cout<< endl << endl << endl << endl << endl << endl
<< setw(30)<<"First Name : ";
cin>>emp[i].PatientFname;
outputFile <<emp[i].PatientFname <<",";
cout<< setw(30)<<"Last Name : ";
cin>>emp[i].PatientLname;
outputFile <<emp[i].PatientLname <<",";
cout<< setw(30)<<"Age : ";
cin>>emp[i].Age;
outputFile <<emp[i].Age <<",";
}
int main ()
{
char response;
for (int i = 0; i < 50;) {
cout << "Do you want to add another person?";
cin >> response;
if (response == 'y')
makeBooking(i++);
else if (respinse == 'n')
break;
else
cout << "Undefined response";
}
return 0;
}
I'm writing a basic random number guessing game, and I'm trying to perfect it a bit when it comes to entering illegal characters, and as long as numbers outside of the range 1-100 are entered the program tells the user and the user gets to redo it, same goes with letters. However, if you enter 23x5 you end up getting double error messages, you get both the letter and a too high/too low depending on the random number. How do I sort it out so that this entry would go under the letter error message as well?
Here's my code:
Header.h
#ifndef HEADER_H
#define HEADER_H
int nGuessedNumber;
int nNumberOfGuesses = 1;
int nRandomNumber;
int UserInput();
#endif
Source.cpp
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
extern int nGuessedNumber;
int UserInput()
{
while(!(cin >> nGuessedNumber))
{
cin.clear();
while(cin.get() != '\n'){}
cout << "I asked for a number between 1 and 100.\n";
}
return nGuessedNumber;
}
main.cpp
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Header.h"
using namespace std;
int main()
{
srand(time(0));
nRandomNumber = rand() % 100 + 1;// sets random number between 1 and 100
cout << "Guess a number from 1 too 100: " << endl;
UserInput();
while (nGuessedNumber != nRandomNumber)
{
if ((nGuessedNumber < 1) || (nGuessedNumber > 100))
{
cout << "Oi! Between 1 and 100!\n";
UserInput();
}
else
{
if (nGuessedNumber < nRandomNumber)
{
for (nGuessedNumber; nGuessedNumber < nRandomNumber; nNumberOfGuesses++)
{
cout << "Too low, try again!" <<endl;
UserInput();
}
}
else if (nGuessedNumber > nRandomNumber)
{
for (nGuessedNumber; nGuessedNumber > nRandomNumber; nNumberOfGuesses++)
{
cout << "Too high, try again!"<< endl;
UserInput();
}
}
}
}
if (nGuessedNumber == nRandomNumber)
{
cout << "Congratulations! " << nGuessedNumber << " is correct!" << endl;
cout << "You guessed " << nNumberOfGuesses << " times." << endl;
}
system("PAUSE");
return 0;
}
Read whole lines from std::cin and parse them individually using std::istringstream:
int UserInput()
{
std::string line;
while (getline(std::cin, line)) {
std::istringstream is(line);
if (is >> nGuessedNumber) {
...
return nGuessedNumber;
} else {
...
}
}
}