In case you like math, you can try the following site:
http://projecteuler.net
siddhant3s commented: Neat +4
unbeatable0 32 Junior Poster in Training
Salem commented: Well said +25
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;
}
const int MAX = 13; int a[MAX]; for (int i = 0; i < MAX; ++i) a[i] = i; for (int i = 0; i < MAX; ++i) { int r = rand() % MAX; while(r == i) r = rand() % MAX; int t = a[i]; a[i] = a[r]; a[r] = t; }
That oughta take care of the first thing. ;D Shuffling the same elements twice is something that has in chance in real life as well, so it's part of being random. :)
I didn't say it isn't random, I just said he can't ensure that every element moves at least once ;)
Change this line: cout<<fly.freeSeat;
to this: cout<<fly.freeSeat();
Remember that freeSeat
is a function ;).
Edit: I guess Narue was faster :)
Here's a simple shuffle that ensures every element is moved at least once. Note the loop only needs 13 reps for 13 numbers.
const int MAX = 13; int a[MAX]; for (int i = 0; i < MAX; ++i) a[i] = i; for (int i = 0; i < MAX; ++i) { int r = rand() % MAX; int t = a[i]; a[i] = a[r]; a[r] = t; }
You can't ensure that this code will move every element at least once because you can't ensure that r=rand()%MAX;
won't give the exact value of i.
The code might also replace twice the same elements, causing no change.
There are free-to-read tutorials on the Internet as well.
Two good sites:
http://cprogramming.com
http://cplusplus.com
In addition to the above posts:
1) The user has no chance to input the second number. Better add cin>>dsecondnumber;
after the user is told to input the second number.
2) In your case
statements the output is always
cout<< " The Answer is:"<< dfirstnumber << (operator) << dsecondnumber << dfirstnumber << "=" << dfirstnumber (operator) dsecondnumber << endl;
Why do you output the first number again? The output for 5*6 now (with that unnecessary output addition) is "The Answer is:5*65=30".
3) Change this:
default:
cout<< " That is an invalid operation" << endl;
break;
{
cout<< " Would you like to start again? (Y or N)" << endl;
cin>> cDoagain;
to
default:
cout<< " That is an invalid operation" << endl;
break;
}
cout<< " Would you like to start again? (Y or N)" << endl;
cin>> cDoagain;
4)
Just for comfort - you could write this:
case '*':
cout<< " The Answer is:"<< dfirstnumber << "*" << dsecondnumber
<< "=" << dfirstnumber * dsecondnumber
<< endl;
break;
case 'x':
cout<< " The Answer is:"<< dfirstnumber << "x" << dsecondnumber
<< "=" << dfirstnumber * dsecondnumber
<< endl;
break;
case 'X':
cout<< " The Answer is:"<< dfirstnumber << "X" << dsecondnumber
<< "=" << dfirstnumber * dsecondnumber
<< endl;
break;
at bit shorter:
case 'x':
case 'X':
case '*':
cout<< " The Answer is:"<< dfirstnumber << "*" << dsecondnumber
<< "=" << dfirstnumber * dsecondnumber
<< endl;
break;
That's weird - it works perfect for me.
Try something like this instead of your while(encrypt!=13)
loop:
while(encrypt != 13)
{
if(encrypt!='\b')
{
Password.push_back(encrypt);
cout << '*';
}
else if(Password.length()>0) // Backspace only when possible
{
Password.resize(Password.length()-1); // Will also erase the last character
cout<<"\b \b"; // Inserts a space in place of the previous asterisk and moves to the desired location
}
encrypt = _getch();
}
What about a linked list?
Or alternatively, you could store all the class instances in an array with as many elements as the the user inputs, and to check which instance has the student's ID run a loop through all the indexes.
An example to make it all clearer:
...
struct StudentInfo
{
...
unsigned int ID;
...
}
int main(void)
{
short int NumOfStudents;
cout<<"Input information on how many students?\n";
cin>>NumOfStudents;
StudentInfo Students[NumOfStudents];
short int Index;
for(Index=0;Index<NumOfStudents;Index++)
{
... // input students information
}
unsigned int ID_ToLookFor;
cout<<"Search for a student with what ID?\n";
cin>>ID_ToLookFor;
for(Index=0;Index<NumOfStudents;Index++)
{
if(Students[Index].ID==ID_ToLookFor) .. // do whatever you need to do here
}
...
}
Add Password="";
before
while(encrypt != 13)
{
Password.push_back(encrypt);
cout << '*';
encrypt = _getch();
}
That's because when the user enters a wrong password you keep Password
with the wrong password and just add characters to it. You need to empty it before.
Look at your while loop: you closed it before it started: while(ordnum != 99999);
.
Just remove the semicolon right after the while(ordnum != 99999)
and everything should be fine ;)
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;
Hi all,
what is the bug in the following code:#include <iostream> #include <string> int main() { string str = "Hello World!" cout << str << endl; cout << float x = 5.0f * str << endl; int 65Num = 65; cout << "65Num = " << 65Num << endl; }
There are a lot of "bugs" in your code.
Frist, you miss the using namespace std;
line before the main()
function, so the compiler might not recognize cout
and string
.
You also missed a semicolon after "Hello World!"
.
Another thing is that you can't declare variables inside cout
, so you should take float x = 5.0f * str
outside cout
. The multiplication operation of a float type and string type is not even declared, so float x = 5.0f * str
has no meaning. The variable 65Num
has an "illegal" name - variables' names cannot begin with a digit.
I agree that the sum is different. I said so before. All I say is that the result of what we're interested in, in this case, remains the same - just because the ASCII value of '0' modulo 2 is 0.
We're not interested in the sum of the digits. We're interested to know if the number's sum of digits is even or odd, or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
We're talking about the result of the sum modulo 2. The result is the same.
Nope. It doesn't change the answer of Sum%2 simply because '0' mod 2 = 0.
If x=y (mod n), and z=0 (mod n), x+z will still equal y (mod n).
ASCII '0' is 48, '1' is 49... '9' is 57 - '0' mod 2 remains 0, '1' mod 2 remains 1, ... , '9' mod 2 remains 1.
It's working for determining if the sum is even or not. The Sum mod 2 remains the same.
Running a loop for such a purpose will look like this using mathematical operations:
unsigned short int a,Sum=0;
a=12522;
while(a)
{
Sum+=a%10;
a/=10;
}
Using string operations:
string a;
unsigned short int Sum=0,i;
a="12522";
for(i=0;i<a.length();i++) Sum+=a[i];
I personally prefer the second option when you assume ASCII and don't need a speedy algorithm - for example in this program. It looks clearer for me - more self-explanatory.
It doesn't change much, however. It doesn't worth an argument.
I actually don't understand why you need a string for that purpose, you can do it using mathematical operations only.
Why making it difficult if it can be easy ?Look at ArkM's post ! :)
When you don't need to do calculations with the number there's no reason for doing the mathematical operations on it - you have the possibility to separate the characters more easily usign strings.
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?.
I don't understand - why do you have the numbers input as 'int'? Why don't you have the numbers input directly into a string and then use the operator [] to move through the digits and count values? I think it could be much more efficient - just run a loop like for(index=0;index<number.length();index++)
and add every digit to a variable that holds the sum. If you assume ASCII, you won't even need to convert the character digits into digits, because their ASCII values are even for even digits.
You'll have to re-write everything. Just make a new file, read the file you want, and write the lines to the new file, with the changes you want. Afterwards, erase the old file and rename the new file to the old file's name.
There's something similar (erasing a line from a file) in the code snippets. Feel free to dig there ;)
Whoops, I guess I was a bit slow since I looked for the link to that snippet in the snippets section :)
What do you mean? Do you want the blocks to appear in lines, like that?:
****** ****** ****** ****** ******
* * * * * * * * * *
* * * * * * * * * *
****** ****** ****** ****** ******
If you do, you need to print multiple blocks drawing-lines before finished the line.
Binary operations are operations between two variables (like a+b, a-b, a^b, a/b etc.).
Unary operations are operations on one variable (a++, a-- ...)
Where do you get an 8 byte long int? It's 4 bytes everywhere I've seen.
A long long int
(signed/unsigned) is 8 bytes. cout<<sizeof(long long)
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.
....
Are there any good online sources you all would recommend for a beginner?Thannks,
Tom
The following site is good, but of course won't cover much.
I personally learn only from the internet, and make programs for fun and complex calculations. Whenever I want to do something I don't know how, I look over the internet for it. "Google is your friend" :)
http://newdata.box.sk/bx/c/
A much clearer method would be rand()%x
, where x is the number you want. For example, rand()%7
will return a random number between 0 and 6, and the code rand()%7+1
will return a value between 1 and 7.
If you want the numbers to be as random as possible, add the line srand(time(NULL));
before calling rand() (it's only necessary to use it once).
For more information about srand() and rand() you can read:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html
The computer doesn't recognize when a cell is occupied because you didn't tell it to.
You're making a check only on board[5][x]. You should use a loop to check which is the the highest place available and just then test it.
http://www.daniweb.com/forums/post31214.html
This link should help you. It teached me well how to use ifstream objects.
We won't do your homework. You should provide us some code so we can know you've at least tried.
http://www.daniweb.com/forums/announcement8-2.html
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;
}
I think the following snippet is more efficient (using the ideas of the above code by Chris):
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main(void)
{
int InputValue=0,x;
string Input;
cout<<"Input a string to check its value:\n";
getline(cin,Input);
for(x=0;x<Input.length();x++)
{
if(isalpha(Input[x]))
{
InputValue+=toupper(Input[x])-'A'+1;
cout<<toupper(Input[x])-'A'+1<<"+";
}
}
cout<<"\b="<<InputValue<<"\n";
system("pause");
return 0;
}
Just to bring it to your attention: 3+1+2 = 6, not 5 :P
sorry I dont understand you.. i have that code in my programme..! :)
Functions should be declared outside the main() function.
Example:
long int factorial(long int);
int main(void)
{
// blah blah blah
} // main() ends
/*now you can declare another function.
Can also be before main(), but not inside it */
long int factorial(long int i)
{
// now you can write the function
}
thx so much guy..i have almost made it.... i have to give an error msg id name contains an integer or a symbol....i have tries the while loop but it doesnt works....can ne1 chk
// you forgot add the following: #include<iostream> #include<string> using namespace std; int main(int argc, char *argv[]) { string n; string s; { // <--- this bracket is unnecessary cout<<"Please enter your name :"<<flush; getline(cin, n); for(int i=1; i<n.length(); i++) /* while(n[i]<='A'|| n[i]>='z'); cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; cout<<"Please enter your name :"<<flush; getline(cin, n); */ // correction: if(n[i]>'z' || n[i]<'A' && n[i]!=' ') { cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl; cout<<"Please enter your name :"<<flush; getline(cin, n); } if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A'; for(int x=1; x<n.length(); x++) { if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A'; else if(n[x]>='A' && n[x]<='Z' /* it might be goot to add " && n[x-1]!=' ' " */) n[x]+='a'-'A'; } cout<<n<<endl; }
The green (// and /* */) parts are the corrections.
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;
}
Why bother with the whole word when you only need the first letter ('m' or 'f')?
if(s[0]=='m' || s[0]=='M')
// do what you want for male
else
// do what you want for female
Look at this link:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385
The last method is probably what you're looking for.