Below if the code I'm using. I wonder if anyone knows a faster way to do what I'm doing, which is taking in a file by reading its data byte by byte, and throwing out every other byte (so the final output is 1/2 the original).
I find that that way I'm doing it is really slow. Is there a way to do this faster? I think that reading the file in at once might speed things up so that cin doesn't have to be called over and over, but I don't know how to go about doing that.
#include%26lt;iostream%26gt;
using namespace std;
main()
{int i=0;
char ch;
while(cin.get(ch))
{if(i == 1)
{cout %26lt;%26lt; ch;
i = 0;
}
else if(i == 0)
{i = 1;
}
else {exit(-1);}
}
}
Is there an efficient way to toss binary data in C++?
You'd be better off reading the entire file into a dynamically allocated array to begin with, then using pointer arithmetic to remove the unnecessary bytes. File accesses are super slow, but if you read the entire thing first you can do it by reading in much bigger chunks and then just iterate over the list of read in data and skip every other byte.
Monday, May 24, 2010
Validating crap in c++?
i'm trying to validate numbers as I enter them in my program. can anyone tell me whats wrong with this?
cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";
cin %26gt;%26gt; numberNum;
if (numberNum != '0','1','2','3','4','5','6','7','8','9' )
{
cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;
}
everytime i run the program, the message pops up. its as if the compiler sees the valids "1,2,3..." as invalid. any suggestions?
Validating crap in c++?
why don't u try this
cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";
cin %26gt;%26gt; numberNum;
if (numberNum %26gt; 20 || numberNum %26lt;0)
{
cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;
cin %26gt;%26gt; numberNum
}
Reply:The comma is a valid C++ operator, but it doesn't do what you're thinking it does. The comma evaluates the expression on its left, discarding the result, then returns the type and value of the expression on the right. Also, you're making your life difficult. For 0-20, it's difficult, but what if you wanted the numbers 0-100 to be valid?! Typing them all out would get insane, and a phone number, a MILLION possible numbers you don't want to type!
use if(numberNum %26gt;= 0 %26amp;%26amp; numbernNum%26lt;=20)
now if you want to expand it to a million all you have to change it to would be
if(numberNum %26gt;= 0 %26amp;%26amp; numberNum%26lt;= 10000000)
(you'd also have to make it an integer, but a search of answers will tell you how to do that).
Reply:instead you need this...
if (numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2') //etc
Perhaps a better way to ensure the validity of the input is to get it as a string using getline, then pass it to a function like atoi() and check for an error.
Reply:This only checks 0. You need :
if ((numberNum %26gt;-1) %26amp;%26amp; (numberNum %26lt;20)
{//routine for a match
}else
{//routine for no match
}
Reply:"numberNum != '0','1','2','3','4','5','6','7'..." is not valid.
You would need to do this: "numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2'..."
Otherwise, instead of using cin, do this:
int numberNum;
scanf(%d , %26amp;numberNum ); //read in the value as an integer
if( numberNum %26lt; 0 || numberNum %26gt; 20) {
....
}
apple
cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";
cin %26gt;%26gt; numberNum;
if (numberNum != '0','1','2','3','4','5','6','7','8','9' )
{
cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;
}
everytime i run the program, the message pops up. its as if the compiler sees the valids "1,2,3..." as invalid. any suggestions?
Validating crap in c++?
why don't u try this
cout %26lt;%26lt; "\nHow many whole numbers (up to 20) would you like to organize? ";
cin %26gt;%26gt; numberNum;
if (numberNum %26gt; 20 || numberNum %26lt;0)
{
cout %26lt;%26lt; "Please enter a valid number: " %26lt;%26lt; endl;
cin %26gt;%26gt; numberNum
}
Reply:The comma is a valid C++ operator, but it doesn't do what you're thinking it does. The comma evaluates the expression on its left, discarding the result, then returns the type and value of the expression on the right. Also, you're making your life difficult. For 0-20, it's difficult, but what if you wanted the numbers 0-100 to be valid?! Typing them all out would get insane, and a phone number, a MILLION possible numbers you don't want to type!
use if(numberNum %26gt;= 0 %26amp;%26amp; numbernNum%26lt;=20)
now if you want to expand it to a million all you have to change it to would be
if(numberNum %26gt;= 0 %26amp;%26amp; numberNum%26lt;= 10000000)
(you'd also have to make it an integer, but a search of answers will tell you how to do that).
Reply:instead you need this...
if (numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2') //etc
Perhaps a better way to ensure the validity of the input is to get it as a string using getline, then pass it to a function like atoi() and check for an error.
Reply:This only checks 0. You need :
if ((numberNum %26gt;-1) %26amp;%26amp; (numberNum %26lt;20)
{//routine for a match
}else
{//routine for no match
}
Reply:"numberNum != '0','1','2','3','4','5','6','7'..." is not valid.
You would need to do this: "numberNum != '0' %26amp;%26amp; numberNum != '1' %26amp;%26amp; numberNum != '2'..."
Otherwise, instead of using cin, do this:
int numberNum;
scanf(%d , %26amp;numberNum ); //read in the value as an integer
if( numberNum %26lt; 0 || numberNum %26gt; 20) {
....
}
apple
Why doesnt my C++ program work? Help!?
ok so im supposed to write a program that calculates n! using functinos, this is what i have so far.
#include %26lt;iostream%26gt;
using namespace std;
int fact(int n, Int factor, int i)
{
for (int i=1;i%26lt;=n;i++)
factor = factor * i;
return factor;
}
int main ()
{
int n, factor, i;
cout%26lt;%26lt;"Enter a number"%26lt;%26lt;"\n";
cin%26gt;%26gt;n;
factor = fact(n,factor,i);
cout%26lt;%26lt;factor;
return 0;
}
i copile it and it checks out but my answer is always zero. Can anyone see what i did wrong?
Why doesnt my C++ program work? Help!?
%26gt; i did but all i got then was an error message, NO feaken
%26gt; WAY!!!! I did before and all i got was an error message
%26gt; i did it just now again and it worked, wtf!!! My comp is
%26gt; messing with me, (Argg)
Welcome to the world of programming! It sucks! You're gonna love it...
Reply:try initializing factor to 1 before calling the function.
Reply:Change the line 'int n,factor,i;' to this:
int n,i;
unsigned long factor=1;
In your original program, 'factor' was not initialized, so the initial value could have been anything. Also, by using an unsigned long data type for 'factor', the program will allow a number up to 4294967295.
You had 'factor' as an integer, so the maximum number it can hold is 32767.
Reply:hey man what are you doing its all wrong let me write that for you. It goes like this........
# include%26lt;iostream.h%26gt;
int fact(int n)
{ int factorial=1;
for (int i=1;i%26lt;=n;i++)
{
factorial=factorial*i;
}
return factorial;
}
int main()
{
int n;
cin%26gt;%26gt;n;
int factorial=fact(n);
cout%26lt;%26lt;factorial;
return 0
}
hope it helps.
#include %26lt;iostream%26gt;
using namespace std;
int fact(int n, Int factor, int i)
{
for (int i=1;i%26lt;=n;i++)
factor = factor * i;
return factor;
}
int main ()
{
int n, factor, i;
cout%26lt;%26lt;"Enter a number"%26lt;%26lt;"\n";
cin%26gt;%26gt;n;
factor = fact(n,factor,i);
cout%26lt;%26lt;factor;
return 0;
}
i copile it and it checks out but my answer is always zero. Can anyone see what i did wrong?
Why doesnt my C++ program work? Help!?
%26gt; i did but all i got then was an error message, NO feaken
%26gt; WAY!!!! I did before and all i got was an error message
%26gt; i did it just now again and it worked, wtf!!! My comp is
%26gt; messing with me, (Argg)
Welcome to the world of programming! It sucks! You're gonna love it...
Reply:try initializing factor to 1 before calling the function.
Reply:Change the line 'int n,factor,i;' to this:
int n,i;
unsigned long factor=1;
In your original program, 'factor' was not initialized, so the initial value could have been anything. Also, by using an unsigned long data type for 'factor', the program will allow a number up to 4294967295.
You had 'factor' as an integer, so the maximum number it can hold is 32767.
Reply:hey man what are you doing its all wrong let me write that for you. It goes like this........
# include%26lt;iostream.h%26gt;
int fact(int n)
{ int factorial=1;
for (int i=1;i%26lt;=n;i++)
{
factorial=factorial*i;
}
return factorial;
}
int main()
{
int n;
cin%26gt;%26gt;n;
int factorial=fact(n);
cout%26lt;%26lt;factorial;
return 0
}
hope it helps.
Cannot get this C++ program to compile using bounds.. help plz!?
Okay, this program consists of repeated calls to 2 functions to compute the sum and product of all integers between two bounds. For my example, the user is imputing 3 for lower bound, and 7 for upper bound.
I cannot get this program to compile, and suggestions would be great! I will need to add a count controlled loop to this program later, I will just have to try to figure that out once i can get this portion to work though.
My code so far:
#include %26lt;iostream%26gt;
using namespace std;
int main();
int sum(int lower, int upper){
cout %26lt;%26lt; " Enter a lower and upper bound: ";
cin %26gt;%26gt; lower %26gt;%26gt; upper;
int total = 0; //holds the total that will be returned
for(int i = lower; i %26lt;= upper; i++); //calculate the total
total += i; //returns the total, which is the sum 25 (3 + 4 + 5 + 6 + 7)
return total;
}
Thanks in advance!!
Cannot get this C++ program to compile using bounds.. help plz!?
First you ask for product and now you are asking about sum,. It would be better if you do it yourself
Reply:this should work
#include %26lt;iostream%26gt;
using namespace std;
int getNums(int %26amp;lower, int %26amp;upper);
int sum(int, int);
int main(){
int upper=0, lower=0, total=0;
getNums(lower, upper);
total = sum(upper, lower);
return 0;
}
int sum(int upper, int lower){
int total=0;
for(int i=lower; i%26lt;=upper; i++)
i+=total;
return total;
}
int sum(int %26amp;lower, int %26amp;upper){
cout %26lt;%26lt; "Type a lower and upper bounds:\n";
cin %26gt;%26gt; lower;
cin %26gt;%26gt; upper;
return 0;
}
Reply:In C++, to get this to compile you have a bad arrangement of braces that must be remedied. I'll show them in a better light below.
#include %26lt;iostream%26gt;
using namespace std;
int sum(void){//definition of function. { required.
int lower, upper;
cout %26lt;%26lt; " Enter a lower and upper bound: ";
cin %26gt;%26gt; lower %26gt;%26gt; upper;
int total = 0; //holds the total that will be //returned
for(int i = lower; i %26lt;= upper; i++){ //calculate the //total
total += i; //returns the total, which is the sum //25 (3 + 4 + 5 + 6 + 7)
}//end of for loop.
return total;
} // end of function sum.
int main(){
cout %26lt;%26lt; sum();
return 0;
}// end of main.
I've got it so it will compile, so start in from there and reread any material you've got on what demarcates a function in C/C++. I hope this helps. Pivy.
I cannot get this program to compile, and suggestions would be great! I will need to add a count controlled loop to this program later, I will just have to try to figure that out once i can get this portion to work though.
My code so far:
#include %26lt;iostream%26gt;
using namespace std;
int main();
int sum(int lower, int upper){
cout %26lt;%26lt; " Enter a lower and upper bound: ";
cin %26gt;%26gt; lower %26gt;%26gt; upper;
int total = 0; //holds the total that will be returned
for(int i = lower; i %26lt;= upper; i++); //calculate the total
total += i; //returns the total, which is the sum 25 (3 + 4 + 5 + 6 + 7)
return total;
}
Thanks in advance!!
Cannot get this C++ program to compile using bounds.. help plz!?
First you ask for product and now you are asking about sum,. It would be better if you do it yourself
Reply:this should work
#include %26lt;iostream%26gt;
using namespace std;
int getNums(int %26amp;lower, int %26amp;upper);
int sum(int, int);
int main(){
int upper=0, lower=0, total=0;
getNums(lower, upper);
total = sum(upper, lower);
return 0;
}
int sum(int upper, int lower){
int total=0;
for(int i=lower; i%26lt;=upper; i++)
i+=total;
return total;
}
int sum(int %26amp;lower, int %26amp;upper){
cout %26lt;%26lt; "Type a lower and upper bounds:\n";
cin %26gt;%26gt; lower;
cin %26gt;%26gt; upper;
return 0;
}
Reply:In C++, to get this to compile you have a bad arrangement of braces that must be remedied. I'll show them in a better light below.
#include %26lt;iostream%26gt;
using namespace std;
int sum(void){//definition of function. { required.
int lower, upper;
cout %26lt;%26lt; " Enter a lower and upper bound: ";
cin %26gt;%26gt; lower %26gt;%26gt; upper;
int total = 0; //holds the total that will be //returned
for(int i = lower; i %26lt;= upper; i++){ //calculate the //total
total += i; //returns the total, which is the sum //25 (3 + 4 + 5 + 6 + 7)
}//end of for loop.
return total;
} // end of function sum.
int main(){
cout %26lt;%26lt; sum();
return 0;
}// end of main.
I've got it so it will compile, so start in from there and reread any material you've got on what demarcates a function in C/C++. I hope this helps. Pivy.
Syntax error in c++ program?
while () (ch=cin.get) ! =\n
Syntax error in c++ program?
while ( (ch = cin.get()) != '\n') { }
Reply:while ( (ch = cin.get) != \n) { }
Reply:while () (ch=cin.get) ! ="\n"
Syntax error in c++ program?
while ( (ch = cin.get()) != '\n') { }
Reply:while ( (ch = cin.get) != \n) { }
Reply:while () (ch=cin.get) ! ="\n"
What is wrong with this C++ program?
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
int numCount, total;
double average;
cout %26lt;%26lt; "How many numbers do you want to average? ";
cin %26gt;%26gt; numCount;
for (int count = 0; count %26lt; numCount; count++)
{
int num;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; num;
total += num;
}
average = total / numCount;
cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;
return 0;
}
What is wrong with this C++ program?
dude int total get initialized to garbage value..
so use
int numCount,total=0;
this should giv u the correct answer
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
int numCount, total=0;
double average;
cout %26lt;%26lt; "How many numbers do you want to average? ";
cin %26gt;%26gt; numCount;
for (int count = 0; count %26lt; numCount; count++)
{
int num;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; num;
total += num;
}
average = total / numCount;
cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;
return 0;
}
Reply:total/numCount is integer devided by integer wil result in fraction part truncated. Also all vaiables need to be initialized to 0.
Two Solutions
1. Use datatype casting: i.e., You can replace
average = total/numCount ;
with
average=(double)total / (double) numcount;
2. Initialize numcount and Total as double/float datatype
instead of integer.
Reply:It looks ok to me. The only thing I would suggest is initializing your variables to something so you know you don't have junk in them. So set numCount and total to 0 at the top and also set average to 0.0 and don't forget to set num = 0 in your for loop as well. I'm not sure exactly what the problem is or if there is one since you didn't mention a specific error.
Reply:Try placing the "int num;" outside of the for.
for total, make it "total = total + num;"
Remember to close everything, even "for" with a ";"
If this doesn't fix it, can you say what exactly the problem is when it runs?
It's been a long time since C++, sorry if I'm wrong.
augustifolia
using namespace std;
int main()
{
int numCount, total;
double average;
cout %26lt;%26lt; "How many numbers do you want to average? ";
cin %26gt;%26gt; numCount;
for (int count = 0; count %26lt; numCount; count++)
{
int num;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; num;
total += num;
}
average = total / numCount;
cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;
return 0;
}
What is wrong with this C++ program?
dude int total get initialized to garbage value..
so use
int numCount,total=0;
this should giv u the correct answer
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
int numCount, total=0;
double average;
cout %26lt;%26lt; "How many numbers do you want to average? ";
cin %26gt;%26gt; numCount;
for (int count = 0; count %26lt; numCount; count++)
{
int num;
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; num;
total += num;
}
average = total / numCount;
cout %26lt;%26lt; "The average is " %26lt;%26lt; average %26lt;%26lt; endl;
return 0;
}
Reply:total/numCount is integer devided by integer wil result in fraction part truncated. Also all vaiables need to be initialized to 0.
Two Solutions
1. Use datatype casting: i.e., You can replace
average = total/numCount ;
with
average=(double)total / (double) numcount;
2. Initialize numcount and Total as double/float datatype
instead of integer.
Reply:It looks ok to me. The only thing I would suggest is initializing your variables to something so you know you don't have junk in them. So set numCount and total to 0 at the top and also set average to 0.0 and don't forget to set num = 0 in your for loop as well. I'm not sure exactly what the problem is or if there is one since you didn't mention a specific error.
Reply:Try placing the "int num;" outside of the for.
for total, make it "total = total + num;"
Remember to close everything, even "for" with a ";"
If this doesn't fix it, can you say what exactly the problem is when it runs?
It's been a long time since C++, sorry if I'm wrong.
augustifolia
Sos, plz solve the errors c++(core)?
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
main()
{
float a,b,sign;
cout%26lt;%26lt;"enter two numbers";
cin%26gt;%26gt;a%26gt;%26gt;b;
cout%26lt;%26lt;"choose a sign";
cin%26gt;%26gt;sign;
if (sign==1)
{sign=a*b;
cout%26lt;%26lt;sign;}
if (sign==2)
{sign=a+b;
cout%26lt;%26lt;sign;}
if (sign==3)
{sign=a/b;
cout%26lt;%26lt;sign;}
if (sign==4)
{sign=a-b;
cout%26lt;%26lt;sign;}
getch()
}
}
Sos, plz solve the errors c++(core)?
what kind of math is that??????????
ummmmmmm.................................
dont know sorry
Reply:#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
main()
{
float a,b,sign;
cout%26lt;%26lt;"enter two numbers";
cin%26gt;%26gt;a%26gt;%26gt;b;
cout%26lt;%26lt;"choose a sign";
cin%26gt;%26gt;sign;
if (sign==1)
{sign=a*b;
cout%26lt;%26lt;sign;}
if (sign==2)
{sign=a+b;
cout%26lt;%26lt;sign;}
if (sign==3)
{sign=a/b;
cout%26lt;%26lt;sign;}
if (sign==4)
{sign=a-b;
cout%26lt;%26lt;sign;}
cout%26lt;%26lt;flush;
getch();
}
// Note : you can remove cout%26lt;%26lt;flush; it forces the cout to write on the screen before the pause caused by getch();
Reply:Unless you can be more specific regarding an error (logical) ... as I do not understand the intent of the code other than the obvious ... there are two syntax errors :
(1) Missing semicolon after "getch()"
(2) An excess brace after the line mentioned above.
I would also clean up the code by using a simple switch statement and a few other minor adjustments, as follows :
#include %26lt;conio.h%26gt;
#include %26lt;iostream.h%26gt;
void main()
{
float a, b, result;
int operator;
cout %26lt;%26lt; "Enter two numbers :";
cin %26gt;%26gt; a;
cin %26gt;%26gt; b;
cout %26lt;%26lt; "Select an operator.";
cout %26lt;%26lt; "1. Multiplication";
cout %26lt;%26lt; "2. Addition";
cout %26lt;%26lt; "3. Division";
cout %26lt;%26lt; "4. Subtraction";
cin %26gt;%26gt; operator;
switch (operator)
{
case 1 :
result = (a * b);
break;
case 2 :
result = (a + b);
break;
case 3 :
result = (a / b);
break;
case 4 :
result = (a - b);
break;
}
cout %26lt;%26lt; "Result = " %26lt;%26lt; result;
getch();
}
#include%26lt;conio.h%26gt;
main()
{
float a,b,sign;
cout%26lt;%26lt;"enter two numbers";
cin%26gt;%26gt;a%26gt;%26gt;b;
cout%26lt;%26lt;"choose a sign";
cin%26gt;%26gt;sign;
if (sign==1)
{sign=a*b;
cout%26lt;%26lt;sign;}
if (sign==2)
{sign=a+b;
cout%26lt;%26lt;sign;}
if (sign==3)
{sign=a/b;
cout%26lt;%26lt;sign;}
if (sign==4)
{sign=a-b;
cout%26lt;%26lt;sign;}
getch()
}
}
Sos, plz solve the errors c++(core)?
what kind of math is that??????????
ummmmmmm.................................
dont know sorry
Reply:#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
main()
{
float a,b,sign;
cout%26lt;%26lt;"enter two numbers";
cin%26gt;%26gt;a%26gt;%26gt;b;
cout%26lt;%26lt;"choose a sign";
cin%26gt;%26gt;sign;
if (sign==1)
{sign=a*b;
cout%26lt;%26lt;sign;}
if (sign==2)
{sign=a+b;
cout%26lt;%26lt;sign;}
if (sign==3)
{sign=a/b;
cout%26lt;%26lt;sign;}
if (sign==4)
{sign=a-b;
cout%26lt;%26lt;sign;}
cout%26lt;%26lt;flush;
getch();
}
// Note : you can remove cout%26lt;%26lt;flush; it forces the cout to write on the screen before the pause caused by getch();
Reply:Unless you can be more specific regarding an error (logical) ... as I do not understand the intent of the code other than the obvious ... there are two syntax errors :
(1) Missing semicolon after "getch()"
(2) An excess brace after the line mentioned above.
I would also clean up the code by using a simple switch statement and a few other minor adjustments, as follows :
#include %26lt;conio.h%26gt;
#include %26lt;iostream.h%26gt;
void main()
{
float a, b, result;
int operator;
cout %26lt;%26lt; "Enter two numbers :";
cin %26gt;%26gt; a;
cin %26gt;%26gt; b;
cout %26lt;%26lt; "Select an operator.";
cout %26lt;%26lt; "1. Multiplication";
cout %26lt;%26lt; "2. Addition";
cout %26lt;%26lt; "3. Division";
cout %26lt;%26lt; "4. Subtraction";
cin %26gt;%26gt; operator;
switch (operator)
{
case 1 :
result = (a * b);
break;
case 2 :
result = (a + b);
break;
case 3 :
result = (a / b);
break;
case 4 :
result = (a - b);
break;
}
cout %26lt;%26lt; "Result = " %26lt;%26lt; result;
getch();
}
Subscribe to:
Posts (Atom)