Two things:
First, you forgot to add the first number to the total.
Second, with while(!source_file.eof())
you run through the last line twice.
Therefore, instead of
10+20+40+10+12 = 92
you get
20+40+10+12+12 = 94
Two things:
First, you forgot to add the first number to the total.
Second, with while(!source_file.eof())
you run through the last line twice.
Therefore, instead of
10+20+40+10+12 = 92
you get
20+40+10+12+12 = 94
Look at the code here:
if (pos != -1)
{
goto badsearch;
}
I think you meant:
if (pos == -1)
{
goto badsearch;
}
Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1, so the code should be:
if (pos == string::npos)
{
goto badsearch;
}
cout << search;
pos = search.find("=");
if (pos != string::npos)
{
cout << "\n" << pos;
}
And one last thing: you can avoid using goto
in your 'if' statement by replacing it with the statement continue;
- this will jump to the beginning of the while loop:
if (pos == string::npos)
{
continue;
}
That's weird - it works perfect for me.
The function is expecting an int[] (array/pointer) type and you pass an int, instead you should write the function as: void bookInfo(char[14],char[51],char[31],char[31],char[11],int,double,double);
It would also be better if you used strings from the <string> header instead of arrays of char
s.
outputFile<<"Name: "<<Pname<<" "<<outputFile<<"Date:"<<TDate<<endl;
You output what outputFile returns. This line should be
outputFile<<"Name: "<<Pname<<" "<<"Date:"<<TDate<<endl;
I'm guessing it's because of the getline() function. When I replace the
if(AWUTD=='y'||AWUTD=='Y')
{
cout<<"Please Specify:"<<endl;
getline (cin, AWUTDspecification);
}
with
if(AWUTD=='y'||AWUTD=='Y')
{
cout<<"Please Specify:"<<endl;
cin>>AWUTDspecification;
}
it works better. I suggest that you read the following ReadMe thread: How do I flush the input stream?.
Please use code tags. Otherwise it'll be hard for us to tell you on which lines your mistakes are.
It doesn't matter that 'i' doesn't exist when main() ends and the thread is using it, simply because when main() ends the whole program shuts down, including the threads.
Lines 40-41 - it's a STRING, not a float. I've never heard of a number with two decimal dots.
Also, you didn't end the 'for' loop before the input_type check.
You can see it yourself.
That's because you removed this piece of code from your code:
else if(s[x]=='.')
{
if(!DotReached) DotReached= true;
else
{
input_type=IS_FLOAT;
cout<<"ITs a Float";
break;
}
}
You have a few errors:
Firstly, the if(input_value==0)
..... should be OUTSIDE the 'for' loop, because you want to check what kind of number the input is after you know it isn't a string nor a char.
Therefore you need to end the loop before this 'if' and remove the 'else' before it.
Secondly, in lines 38-39, the type is IS_STRING, because a number can't have more than a single dot.
Now just return the code you've put in the comments. It's necessary!
Lastly, you don't need the break;
in lines 49 and 55, as they're not supposed to be in a loop
for(x=0;x<s.length();x++)
{
if((s[x]>'9' || s[x]<'0') && s[x]!='.')
{
input_type=IS_STRING; break;
cout<<" Its an integer ";
}
else if(s[x]=='.')
{
if(!DotReached) DotReached= true; else
{
input_type=IS_STRING;
cout<<"ITs a Float";
break;
}
}
}
Line 6:
How do you want the program to output "It's an integer" when there's a "break;" before it?
You should output "It's a string" and not "It's an integer" if the input_type is IS_STRING.
Line 14:
Again, it's a string, not a floating-point number.
If you want to determine the input type after you know it's not a char/string (if input_type==0, which is its initial value), you should just check if DotReached returns true or false, because you already know it's a number:
if(input_type==0) // the initial value - meaning no type is determined yet
{
if(DotReached) input_type=IS_FLOAT;
else input_type=IS_INTEGER;
}
Why should a constructor fail?
When you declare a constructor, all the instances of the class will have to use it, or variations of it (if you declare some).
I think it would be much smarter to use the built-in function from <algorithm> header file -- random_shuffle():
Run a loop and insert to the array all the numbers between 1 and 10,000, then call random_shuffle() to shuffle them.
An example code:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main(void)
{
unsigned short int Numbers[10000],x;
for(x=0;x<10000;x++)
{
Numbers[x]=x+1;
}
// shuffle all the Numbers[] array - from element [0] to [9999]
random_shuffle(Numbers, Numbers+10000);
ofstream fout;
fout.open("Numbers1to10000.txt");
for(x=0;x<10000;x++)
{
fout<<Numbers[x]<<"\n"; // put in file
cout<<Numbers[x]<<"\n"; // show to console screen
}
fout.close();
cout<<"Done\n";
cin.get();
return 0;
}
So, you actually want to round up when (the number) mod 1000 is equal or greater than 500, and down if (the number) mod 1000 is less than 500.
An example to how you could write it:
#include<iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Enter a number to round: ";cin>>x;
cout<<"The rounded number is ";
if(x%1000<500) x-=x%1000;
else x+=1000-x%1000;
cout<<x<<"\n";
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
#define IS_STRING 1
#define IS_CHARACTER 2
#define IS_INTEGER 3
#define IS_FLOAT 4
int main(void)
{
string Input;
cout<<"Input a string/character/number:\n";
cin>>Input;
short int input_type=0;
int x;
bool DotReached=false;
if(Input.length()==1 && (Input[0]>'9' || Input[0]<'0'))
input_type=IS_CHARACTER;
else
for(x=0;x<Input.length();x++)
{
if((Input[x]>'9' || Input[x]<'0') && Input[x]!='.')
{input_type=IS_STRING; break;}
else if(Input[x]=='.')
{if(!DotReached) DotReached=ture; else
{input_type=IS_STRING; break;}
}
}
// continue from here
}
Input to a string, then call a function to check whether it the input string has only digits (and '.', '+' and '-' as first only). If it really has only digits, it's a number. If it has a dot, it's a floating-point number, else it's an integer.
If you reach a character that isn't one of the above, it's simply a string. Furthermore, if the string only has one character, it's a char.
After determining what's the user input, you can use the atoi (ASCII to int) or atof (ASCII to float) to convert the string to a number (if the input is a number, of course)