Use [ code] [ /code] tags. You are missing a the while part
in your do while loop.
Using goto will get you killed (not really) in the c++ community.
Use [ code] [ /code] tags. You are missing a the while part
in your do while loop.
Using goto will get you killed (not really) in the c++ community.
You ask the user to give you a number so you can
show every multiple answers of that number.
so int your loop check if the index 'i' is a multiple of that number,
if so then print the answer. Using the mod operator gives
the remainder of the division. If the remainder is a 0 , then the
number is a multiple of that number.
for example : 6 % 2 = 0. because 2 goes into 6 3 times with no
remainder.
Here is an example :
int howMany = 0;
cin >> howMany;
for(int i = 0; i < 100; i ++)
if(i % howMany == 0) cout << i;
I have changed a little to your code ( which I mention in the code) :
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//function to read inputs from the user test for proper input
//and return the value to main if input is invalid,
//the user will be repormpted for proper input
int validInput ( ) //CHANGED
{
int userIn1 = 0;//ADDED
int displayCount = 0;
string userInput = "Please enter the number of terms to use: ";
cout << userInput;
cin >> userIn1; cout << endl;
while ( userIn1 < 1)
{
cout << "Error, cannot be 0 or negative" << endl;
cout << userInput;
cin >> userIn1; cout << endl;
}
return userIn1;
}
int main()
{
int numCalc = 0;
int userIn1 = 0;
cout << "This program calculates the value of PI to varying degrees of accuracy " << endl;
cout << endl;
cout << "The accuracy is based on the desired number of calculations requested by user" << endl;
cout << "The user may also decide the sequence of displayed output during calculations" << endl;
cout << endl << endl;
numCalc = validInput ();
cout << endl << endl;
system ("PAUSE");
return 0;
}
But I am not sure which part you are stuck on. What compiler
are you using? Are you saying that this
string userInput = "Please enter the number of terms to use: "; cout << userInput;
does not print the string?
If you want to print the users input then in your main you can
do this :
int main()
{
...
....
numCalc = validInput (userIn1);
cout<<numCalc<<endl;
}
I assume you want something like this :
int getInput ( int input)
{
cin >> input;
cout << "your input : "<<input<<endl;
return input;
}
In your function, you have validInput(int). It should be
int valudInput() // no parameters, since you want ask the user
to input a value inside the function. But the return type
should be an int if you want it to return the user's input.
Also this condition
while ( userIn1 < 1 ) //means the if userInput is 1 or below reject it.
If you want to validate that the user input is a number then
you can do it like so :
int num = 0;
cout<<"Enter a number : ";
cin >> num;
whlie(!cin ) // check if the user inputs a valid data, i.e a number in this case
{
cin.clear(); //clear the stream
while( cin.get() != '\n') //read all the fail input
;
cout<<"\nPlease enter a number : ";
cin >> num;
}
where are you defining the userIn1 in your main function?
Its simple as this
float a = 3.1234567;
float b = int (a * 1000 ) / 1000.0f // now b = 3.123
Yes, and if your compiler does not support the keyword export,
then it has to be in the same file. (note not tested)
For example :
//pair.h
#ifndef PAIR_H_
#define PAIR_H
template<typename Type, template Type2>
class Pair
{
private:
Type first;
Type second;
public:
Pair() : first(0), second(0) { }
Pair(Type a, Type2 b);
void prntAll();
}
template<typename Type, typename Type2)
Pair<Type, Type2> :: Pair(Type a Type2 b) : first(a), second(b) { }
template<typename Type, typename Type2)
Pair<Type, Type2> :: prntAll() { std::cout<<first<<" , "<<second<<"\n";
}
#endif
"I just want to have random numbers which are unique."
So whats the problem. Are you stuck somewhere, trying to implement
this? How about a trial and show us where/if you have problems.
Since its only 5 numbers, you can create an array that holds say 5 elements. Then fill the array only if the number
is unique (by checking the array).
strlen returns a unsigned int. And you can add a decimal to hex.
for example :
int a = 0xe; //14
cout<<a<<endl; //display 14
a = a + 1; //now a = 15 or 0xf
cout<<a<<endl; //display 15
What you are doing is very dangerous!
bool *dat; //create a bool pointer
dat=new bool; //set bool pointing to a address, i.e pointer-to-bool
dat=true; //give value to the pointed address
++dat; //increase the address of bool. Huh-oh, but where is the
//next address. Even worse, what does that address
//contains? Important data ? maybe
dat=false; //what ever it had now its set to false
delete dat; //now you are deleting a vairable that points to a
//different that what it starts with!
--dat; //Its deleted already, so huh?
Here is one way of doing it.
int Array[5] = {0};
int num(0); //the number the use will input;
for(int i = 0; i < 5; i++)
{
cout<<"Enter value# "<<i<<": ";
cin >> num;
if( num >=5 && num <= 15) //put number into array only if its within bounds
Array[i] = num;
else
{
cout<<"Number should be from 5 to 15. Try again\n";
i = i - 1; //if not subtract 1 from i to reset it
}
}
not sure what you are asking for. Maybe some code would help.
Now try a recursion functions for this job.
1) Find a base case.
2) Find a pattern.
3) Recurse pattern until base case is reached.
Also no need to worry about the little details right now. Just keep it in mind.
Not sure what you mean. Maybe an example would help
Alright. So for the first problem, about the compiler not finding the .txt file--Make sure you download/or include the file where your project is. This solution will be for visual 08.
1) open the project. Then on the left side of the screen(where you have your header file, source file..Right click on the name of the project and go to "open Folder in windows explorer. Then upload the file in the same address.
ok now for your other problems;
The function finduser is a function, that has 3 parameter. When you call the function you have to include 3 argument.
int findUser(User users[], string userID, int noOfUsers)
In your int main()
{
when you call it (ex. findUser(someArray, someString, someInt)
{
you have to use either someArray, someString, or someInt in your function. That is unless you declare another variable in your function, or have a global variable.
so try using the variables declared in the function's parameter.
hope that helps somewhat...
and Merry Christmas.
I have fixed most of your problem. But your function is just missing some identifiers(ex , insted of int x, its just x)..All i did was change your header. look at it.
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int MAX_BOOKS = 1000;
const int MAX_USERS = 500;
enum bookCategory
{
Adventure = 'A',
Detective = 'D',
SciFi = 'S',
Romance = 'R'
};
struct Book
{
string bookID;
string title;
string authorSurname;
string authorFirstname;
string category;
string location;
string onLoan;
};
struct User
{
string userID;
string name;
string houseNo;
string address;
string noOfLoans;
};
int findBookAuthor(Book[], string, int);
int main()
{
fstream bookDataFile;
fstream userDataFile;
Book books[MAX_BOOKS];
User users[MAX_USERS];
int noOfBooks = 0;
int noOfUsers = 0;
bookDataFile.open("bookdata.txt");
userDataFile.open("userdata.txt");
while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS)
{
getline(bookDataFile, books[noOfBooks].authorSurname );
getline(bookDataFile, books[noOfBooks].authorFirstname );
getline(bookDataFile, books[noOfBooks].title );
getline(bookDataFile, books[noOfBooks].category),;
getline(bookDataFile, books[noOfBooks].bookID);
noOfBooks++;
}
while(!userDataFile.eof() && noOfUsers < MAX_USERS)
{
getline(userDataFile, users[noOfUsers].name);
getline(userDataFile, users[noOfUsers].houseNo);
getline(userDataFile, users[noOfUsers].address);
getline(userDataFile, users[noOfUsers].userID);
noOfUsers++;
}
string author;
int bookID;
int option;
while(true)
{
cout << "1. Loan Book" << endl;
cout << "2. Find Book By Author" << endl;
cin >> option;
switch(option)
{
case 1:
cout << "Under Construction" << endl;
break;
case 2:
cout << "Enter Author Name: ";
cin >> author;
bookID = findBookAuthor(books, author, noOfBooks);
break;
}
}
return 0;
}
int findBookAuthor(Book books[], string author, int noOfBooks)
{
for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
{
if(books[bookNo].authorSurname == author)
{
cout << "--------------------------------------------------" << endl; …