VernonDozier: Thanks, I pretty much did the same thing except I just made whatever I wanted to delete equal to "$" and then ignored that element and luckily that worked.
Thanks all for posting!
VernonDozier: Thanks, I pretty much did the same thing except I just made whatever I wanted to delete equal to "$" and then ignored that element and luckily that worked.
Thanks all for posting!
Here is something:
uses
crt;
var
i, ii, control, divcount, count : integer;
begin
clrscr;
readln(control);
for i := 1 to 30000 do
begin
for ii := 1 to i do
begin
if(count=control)then
begin
readln;
exit;
end;
if (i mod ii = 0) then
divcount := divcount + 1;
end;
if divcount = 2 then
begin
writeln(i);
count := count + 1;
divcount := 0;
end
else
divcount := 0;
end;
readln;
end.
Its not in C++, but you can do that...
Hi, is there any function or algorithm that can completely remove a string array element?
For example,
string mystring = 'x', 'xx', 'xx', 'xxx';
if I want to remove 'xx' and I know the position of 'xx' in the array, I want this :
mystring = 'x', 'xxx';
Anything? I think I made it pretty clear? I unsuccessfully tried to just move the unwanted elements towards the end but apparantly, for my problem it didn't work out and ended up causing more problems...
Thank you di2daer and Thirusha for your help! I finally got it to work.
There is a class called BufferedReader which you can use. You can try this code, i had it lying around, so i hope it still works, havent had to do such things in a long time:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); System.out.println(input); br.close();
To use this, which libraries do I have to import? I get those red lines under the two Bufferedreader and InputStreamReader. How do I fix that?
Hello, I am new to Java programming and I just got the NetBeans IDE. One problem that I've been having with Java is the input.
package learning;
public class Main
{
public static void main(String[] args)
{
int num1 = 0, num2 = 0;
cin >> num1 >> num2; // How do I do this in Java??
System.out.println((num1+num2));
}
}
How do I do that in Java? In C++, its too easy. Mainly you never need to use anything other than cin and getline... What do I use in Java??
Thanks
Perhaps this thread will be helpful :
http://www.daniweb.com/forums/thread56329.html
Also, C++ is NOT the only language that allows serial port communications. MAY BE there is a language that allows and makes this process much easier.
Good Luck!!
At what point did I say I was a guy? :P I've learn't C++ when I was about 14, and started C a few month's ago (maybe 10).
I wasn't talking about you. I was referring to the original poster...
"which explains how to program ? i saw something like python and C++ and C .... but i don't know what they mean "
No, it's often not the best place to start from. Lot's of CS teachers in high school are not good. Not all, but lot's. This is probably becouse most of the good one's are teaching university classes. My teacher isn't too bad (I'm also 16), but if I was a complete newbie, I would have suggested to myself to re-learn everything online.
Clearly, the guy here doesn't even know what is C++ and C... I mean no matter how bad your teacher is, he/she wouldn't be hired if they weren't capable of give AT LEAST the basic knowledge of programming and where to start.
You can start online but, you will need great dedication and self motivation to push yourself and not just give up on the basic first problems that you will face.
Take the Computer Science course if your school offers one. That is the best place you can start from.
Good luck!!
When I was a beginer, I was doing random problems in Pascal. Here is one that might help you...
uses
crt;
var
i, control : longint;
input, sum : real;
begin
writeln('Enter the total number of inputs. Then, enter them one by one until you see an output.');
readln(control);
sum := -1000000;
for i := 1 to control do
begin
readln(input);
if input >= sum then
begin
sum := input;
end;
end;
writeln(sum:0:4);
readln;
end.
I also have a program that find the minimum of the given input. But, I'll leave that and the array part for you to work out. After all, I shouldn't have to all the work. :|
Good luck!!
I guess you just need to implement another option in your menus. Like "Press 3 to go to main menu..." then you can use goto ______; and what not... or you can think of something better.
I don't get what you are trying to do though. If you are trying to make like a ticket seller program there are far better ways to do it. For starters, use functions....
I am not sure if you still are monitoring this thread, but when you use <= with ____.length();. It loop one more time. This is caused because I start at 0 and go upto and including the total input character. (Pretty sure that doesn;t make any sense...)
Example:
If the length was 3 then,
it will first loop as 0 then 1 then 2 then 3. In total, thats a total of four times...NOT a bug for this problem but if the use was different it may give you trouble....
First Person: I was just trying to keep it simple because 'Dr.Stupid' (I feel like I am insulting him but I am not...) is a beginer. So yeah, if I had to do something like this, I would have probabaly thought of something different like first converting everything into lower or uppercase and then do some random thing with it...
Wildgoose: I didn't intentionally put errors but I did leave the capital part for him to figure out. When I compile it, it shows me no warnings or errors.
Just curious, what errors are there? Are you talking about the single quote within the double quotes? Or using char i twice?
In C++, if you compare something like R WITHOUT any quotes, it interprets it as a variable. So, I assume that the user will enter R or r. If so, you need to check like
if (input == 'R')
{
}
or like this
if (input == "stop")
{
}
Double quotes are usually used for more than one characters but it doesn't really matter.... Also, read the post that says your boolean logic is incorrect.
Thanks for the assist.
Still not too sure about displaying the number each letter occurs, but here's a new post of my cleaned up code:
The problem you posted says that you need to display how many time a letter of the alphabet occures in the input. Here is a possible but incomplete solution....
#include<iostream>
#include<string>
using namespace std;
int main()
{
string userword;
int count;
cin >> userword; // Read the input from user (Use getline(cin, userword); if you are expecting a sentence with spaces)
cout << "The word '" << userword << "' is " << userword.length() << " characters long." << endl; // Obvious...
cout << "\n\nLower Character Occurances are :" << endl;
for (char i = 'a'; i <= 'z'; i++) // Insted of using integers to loop through this for loop, I am using
{ // characters so I don't have to do unnecessary type casting and all that complicated stuff....
count = 0;
for(unsigned int ii = 0; ii <= userword.length(); ii++) // This for loop is going through all the characters in the string...
{
if(i == userword[ii]) // checks if i occures anytime in the entire string, if it does, it will increment count by 1.
{
count++;
}
} // for loops ends here...
if(! (count == 0)) // If it is 0, there is no point in displaying this information...
{
cout << i << " : " << count << endl; // kinda obvious...
}
}
cout << …
Length of a string can be found using _____.length(); {Fill the space with the name of the variable}
You can then use a for loop to check every single character inside the string and compare it with one another to see if it repeats. While you are doing this, you need to keep track of which letter you have already checked for. (to avoid repeating the same characters when they appear again).
Basically, I THINK this problem can be solved using a simple for loop. I will edit this post and post my solution when I get the VC++ installed on my computer.
EDIT :: Or, you can check for every single letter of the alphabet and display 0 if they don't appear at all. Like check the entire string for 'a' then 'b' and count how many you find and then display the number BEFORE moving on to the next letter. You can also put a if statement that can block any letter with 0 occurance from displaying.
E.G, you check for 'a' and incremement the variable count by one everytime you find 'a'. If you finish counting and the value of count is still 0 then you put a if statement.
if (!(count == 0))
{
display the character's occurance information
}
I am not an expert and I don't fully understand the code there. But, I completed a game written in Pascal where I had to clear only selected pixels on screen. Like all the pixels between (25, 25) and (56, 56).
What I used was clearviewport() in pascal, I did some research for you but couldn't really find a suitable guide. But, I did find that a similar function setviewport() is available in C++.
Setviewport(x1, y1, x2, y2);
It will basicallly set your "screen" to those given coordinated. Imagine drawing a rectangle with its top-left point equal to (x1, y1) and bottom right point equal to (x2, y2);
Originally, when you use cleardevice(); the "screen" it will clear can be set like : setviewport(0, 0, getmaxx, getmaxy); {ALL the pixels}
Now what you can do is, setviewport to a portion of the screen and clear only that portion using cleardevice().
p.s. I am not sure if this is what you are asking but I think it may help....
1. Since you are new to C++, I doubt you will really need to use that argc stuff but like that first post says, you can find information if you really need in that link.
2. "If you divide i by 7 and have the remaider 0 then do something."
if (i) modular (7) is equal to (0) then
{
do this
}
% stands for mod which is basically remainder and == is used to check the right side values against the left side (or vise versa but you get the point...)
no that code is not working
the same error as i have mentioned in my first post.
really? That last code works on my computer, if I remember it correctly, it turned the text into purple. I am guessing the problem is the compiler you are using. Try using Visual C++ Express Edition.
Have you thought of using a photo cell(or a solar cell) and a laser(visible or invisible)? It can also be used to detect movements. If it suits your needs, then programming and other things can be so much easier!
Just a thought.
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << "I take back my first post and ask you to search DaniWeb first!" << endl;
return 0;
}
You can change the value of 5 to anything that you like. I don't know the particular range. I'll leave that to you!
Every thing that you can type or have displayed in the DOS shell has a certain number known as the ASCII value. What the problem is asking is "to display all the characters that can be represented in the given range."
edit:
cout << static_cast<char>(100) << endl;
with iostream libray and std namespace, this will display the character that is represented by the ASCII value 10.
To find the ASCII value of a character,
cout << (int)'i' << endl;
same thing again, with iostream libray and std namespace.
www.wcipeg.com/judge
Try that website. Although, it doesn't teach you the algorithms but it has A LOT of problems for you to solve!
Good Luck!!
P.S. I am not advertising, its a non-profit website run by a bunch of computer science students.
EDIT: Do the lower point problems first (3 pointers) then move onto higher ones when you feel that you understand how to submit the problems properly. BTW, NEVER ask the user for input.
For example, for "aplusb", just do
cin>>a>>b;
not
cout << "enter a and b" << endl;
cin >> a >> b;
The problem can be easiely solved by ROUNDING the output number to a certain degree.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double i = 0.546546, ii = 2.365463847698;
cout << fixed;
cout << setprecision(3) << i * ii << endl;
return 0;
}
edit ::
Here is my version of your code...
#include <cstdio>
#include <cstdlib>
#include <process.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string>
#include <windows.h>
using namespace std;
string str;
int main(int nNumberofArgs, char* pszArgs[]) {
//Coding starts here//
double P; //Product price//
double T = 0.07; //Tax for product//
double TP; //Total price after tax//
double TA; //Tendered//
double M; //Misc//
string choice; //After calc choice//
bridge: //The bridge of the program//
cout <<" *------------------------------------------------*\n"
<<" | Welcome To The Automated Cash Register Program |\n"
<<" *------------------------------------------------*\n\n";
cout <<"Product price $";
cin >> P;
TP = (P * T) + P;//Calculates product price with tax *Here is the problem*//
cout <<"Total price $"<<TP<<endl;
cout <<"Out of $";
cin >> TA;
M=TA-TP;
cout <<"Change due $"<<M<<endl;
invalid:
cout <<"\n Type 'q' and hit enter to quit,\n"
<<"or type 'c'and hit enter to continue: ";
cin >> choice;
if(choice=="q") {
return 0; }
if(choice=="c") {
system("CLS");
goto bridge; }
else {
cout <<"Invalid input, try again.";
goto invalid; }
}
Also, you do not know C++ basics. For example, = and == are different. = Assigns values to a variable and == checks two given conditions for …
Use the textcolor function (it's not standard compliant) from conio.h library:
#include <conio.h>
int main()
{
textcolor(RED);
cputs("Red text in console!");
return 0;
}
http://www.ousob.com/ng/borcpp/ng6c57c.php
try using google next time!!
Hi,
I just wonder if this is possible:
(semi-pseudocode (won't work as planned))
ofstream fout("my_file.txt");
fout << cout;
Looking forward to your reply
I don't think thats possible if I properly understand what you are asking....
If you don't know a lot in C++, why don't you start making simpler games like tic tac toe or Who wants to be a millionaire? As to how to make the games, well TTT is really easy. All you need is couple of if statements and while/for loops. for WWTBM, you should use functions and different loops as you see fit.
I am sure google can provide you with more detailed instructions but be careful, it can also provide you with ready code that will just ruin the learning part...
Good luck!
EDIT : I made who wants to be a millionaire in Pascal for my comp sci asssignmet. If you want to know how I did it, PM me. But, I made it in pascal. I am sure i can help with C++ as well if you want to stick to it!
I don't know any function that can do that but you can write your own like this!!
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
string urwelcome(string input)
{
begining:
for(unsigned int i = 0; i <= input.length(); i++)
{
if(input[i] == ' ')
{
input.erase(i, 1);
goto begining;
}
}
return input;
}
int main()
{
string input;
getline(cin, input);
input = urwelcome(input);
cout << input << endl;
return 0;
}
i don't know exactly why I did the goto begining thing but I remember getting help here with a problem where not all the spaces get removed. I just typed it without even knowing why....so don't ask me why. Good luck finding a function that can do this and if you do please post it here!!
soryy, please delet this??
NVM with all that nonsense.... the problem is the cookie.
Hello, I am new webmaster for my high school's robotics team and I don't understand why the pages load differently in IE8 and FireFox.
http://www.team188.com is the website...
Ok, so here is what the problem is : When I open certain pages in IE8, the area around the curve on the top is the only thing in red. The entire blue part should be red.
The page sets a cookie everytime you load it, which stores either 'uniform' or 'os' (for oldschool), then in the main index file, it checks the cookie and opens the appropriate .css file.
Like this,
<link rel="stylesheet" type="text/css" title="User's style" media="all" href="/core/<?php echo (!$sitetheme)?'uniform':$sitetheme ?>.css" />
now, each style sheet has completely different colours. they both are stored in http://www.team188.com/core/ (either uniform or os .css)
I have messed around for months now and I still don't understand the problem. Can someone help?
Thanks.
EDIT : Also, the problem can easily be fixed by anyone if they refresh the affected page once... Its really strage.. have even tried to set uniform.css as the only stylesheet and completely disable the other one... still doesnt work... :(
EDIT : This is what sets the colours in uniform.css
.curvestack {
background-image:url(grid_uniform.png);
}
html {
color:#000;
background-color:#980101;
background-image:url(grid_uniform.png);
}
grid_uniform is basically two pixel image (I think..) The same goes for os.css but the image name is different (the pic is blue instead of red)
My laptop keeps shutting down when I try to turn it on. It will always stay on until the XP's loading screen appears(the screen where there is a bar kinda thing that makes it look like its loading). It does turn on sometimes but after when it shuts off, I have to do random stupid things like move the power cord, take it out the power outlet and put it back in and then it will suddenly start working. My laptop is about 3 years old from HP and as far as I know, the charger and batteries are out dated.
The battery is completely dead and the charger wire is not broken anywhere. I don't get why this is happening.
the only problem with this is that if the triangle has a horizontal leg and a vertical leg you will have division by 0
(0,4) , (4,1), (0, 1)(4 - 0) / (1 - 1) = undifend
I am pretty sure that problem can be solved by putting that in an 'if statement' if it is solvable, it will else it should output something that you want to output. I haven't tried that though...
An easy way to solve this problem is by find the slope of the three lines and checking if any of the slopes happen to be negative reciprocals of the remaining two...
Slope(m) = (Y2 - Y1) / (X2 - X1)
EDIT :: http://www.1728.com/distance.htm
Take a look at that link, scroll down a bit, it explains in greater detail.
P.S. Why can I not edit my posts??
The distance(d) between any two points on a graph is
d = sqrt( ( (x2-x1)^2 ) + ( (y2-y1)^2 ) )
http://www.1728.com/distance.htm Might help you!!!
I think what you are asking is how add number as long as 100 000 digits.
I think the algorithm you are talking about is basically you developing a piece of code that adds just the way you would add two numbers on a piece of paper. For example, you would start from the last digit of the number (the last digit on the right) and you add them. If the sum of these two digits is bigger than 9 then you carry 1 else you just store the sum of the two digits. You then move one digit left and you repeat the same process while taking the carried digit in account. The end result would be the sum of these two number!!
You also need to take a look at these topic :
> ASCII values of the numbers
> loops
> reversing arrays
> Padding make them equal in length
> Adding and substracting using ASCII values
I can't think of any more but there may be problems that you'll have to get around.....
here i done #include <stdio.h>
#include <iostream>
#include <string>int main()
{
char ch;printf("Enter a character: ");
scanf("%c", &chif( isalpha ( ch ) ) {
if ( isupper ( ch ) ) {
ch = tolower ( ch );printf("The lower case equivalent is %c\n",);
}
else {
ch = toupper ( ch );printf("The upper case equivalent is %c\n", );
}
}
else
printf("The character is not a letter");cin.get();
}
but cannot run ... the error "is comparing signed and unsigned values"
help me to slove this plzzz
Hey,
It would be helpful if you put your code in the code tags and properly indent it.
But from what I can see, you are missing a few brakets and semicolons...
I am not sure what exactly you want to do but, if I understand properly, you want to make sure that the char1 and char2 are always letters('a' to 'z' or 'A' to 'Z')?
If that is what you want then all you need is a while loop.
while(1)
{
cout << "Enter the first character" << endl;
cin >> char1;
if((char1 >= 48 && char1 <= 97) || (char1 >= 65 && char1 <= 90))
{
goto here;
}
}
here:
As you can see, it will keep repeating the while loop until the character is a letter. When it is a letter, it will go to the 'here:' which should be just after the while loop.
Why do you have to do something that complicated??
Here is what I came up with!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string y;
char x;
cout<<"Please enter a string: "<<endl;
cin>>y;
cout<<"Please enter a character: "<<endl;
cin>>x;
cout << endl << endl << endl;
for(int i = 0; i < y.length(); i++)
{
if(y[i] == x)
{
cout << x << " found at " << i << '.' << endl;
cout << "Replacing the " << y[i] << " with 'x'." << endl << endl << endl;
y[i] = 'x';
}
}
cout << "The final string is " << endl << y << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int hours, minutes;
string ampm, choice; // stores AM or PM depending on the value of 'hours'
while(1)
{
cout << "Enter hours and then space then the minutes" << endl;
cin >> hours >> minutes;
if(hours > 12) // If it is bigger than 12 then you take out 12 from it.
{
hours -= 12;
ampm = " PM";
}
else // If not, then do not do anything to it and just make 'ampm' "AM"
{
ampm = " AM";
}
cout << hours << ':' << minutes << ampm << endl; // Output the values
cout << "Would you like to re-run the program? (Yes or No)" << endl;
cin >> choice;
if(! (choice == "yes" || choice == "YES" || choice == "Yes")) // If the choice is NOT yes then it will break out of the infinite loop...
{
break;
}
}
return 0;
}
Why do you need to use this many functions do write such a simple program??
Take a look at what I did,
#include<iostream>
#include<string>
using namespace std;
int main()
{
int hours, minutes;
string ampm; // stores AM or PM depending on the value of 'hours'
cout << "Enter the hour followed by a space followed by the minutes." << endl;
cin >> hours >> minutes;
if(hours > 12) // If it is bigger than 12 then you take out 12 from it.
{
hours -= 12;
ampm = " PM";
}
else // If not, then do not do anything to it and just make 'ampm' "AM"
{
ampm = " AM";
}
cout << hours << ':' << minutes << ampm << endl; // Output the values
return 0;
}
Can this program be shortened any further?
I basicallly need to write a program that reads in two numbers and a char.
eg. 10 + 10
then outputs its value.... The char can either be + or - or *. The number will always be less than 100. and you cannot use semicolons... :(
The input consists of int n which tells me how many test cases and then n number of a, b and c. Output should be on a new line. (ie. I need that endl there)
#include<list.h>
int main(char c,int a,int b,int n)
{if(cin>>n)
while(n!=0)
if(cin>>a>>c>>b){
if(c==42)
if(cout<<a*b<<endl){}
if (c==43)
if(cout<<a+b<<endl){}
if(c==45)
if(cout<<a-b<<endl){}
if(n--){}
}}
that is what I have so far... any help is appreciated =)
185 characters. :P
Hey, thanks
After reading your post one after another, I realized an easy way to fix it, I just needed to switch numbers around depending on which number was bigger :):)
Thanks once again!
A plus B
Given two positive or negative integers A and B, find their exact sum.
This time there are no limitations on A and B (of course, they will fit in memory).
A or B can be as large as 100 000 digits long.
Is that what you want to know? thats the problem statement.
To do that I developed that program.... I am not sure if there is any better way to do it. If there is, I need help understanding it. If there is not and the way I did it is the best way, I need help in the subtraction part of the program when any ONE of the number is negative.
Thanks once again. :)
EDIT >> I just realized that the only thing that I need to do is make a substraction function for when the second number is bigger than the first one.!!
I did not realize that the current substraction works only if the first number is bigger than the second one...
I actually, I understand the adding part but, I don't understand this subtraction part. I tried two different way, and they both failed.
What am I doing wrong??
If everything is wrong, how do you subtract two numbers in strings??