zeroliken 79 Nearly a Posting Virtuoso

1. The instructions are unclear

2. try to post your code

zeroliken 79 Nearly a Posting Virtuoso

When you ask for help here you usually post your code for us to make suggestions and check which part has an error thus should help you figure out how to solve your own code

zeroliken 79 Nearly a Posting Virtuoso

Please post your code so we can see your effort on trying to solve your own problem

It's like your asking us for an immediate answer the way you say it :(

zeroliken 79 Nearly a Posting Virtuoso

Oh yes!

Nothing personal, but I really dislike your code:

1) It's int main(void) or int main(int argc, char *argv[]), never just main or void main.

And always with a return (usually zero) to signify success to the OS and you.

2) Your indentations are an affront to real estate on the monitor. 2 to 5 spaces is plenty per level of indentation. Your style will run you right off the row, with most real programs.

3) Your sort is an OMG! Bug city hotel. It's so long, and so error prone to work with, it's just incredible.

C should be CONCISE, (short, and to the point), not a Rube Goldberg deluxe creation. Simple and clear are secondary goals to accuracy, but important one's nevertheless.

A good sorter should be 5 to 30 lines, depending on what you choose, and what function is doing the swapping.

4) Your braces (opening and closing), should vertically line up on the same column. Yours are FAR offset.

5) Why is an add function, an output function in your comments? BTW, this is how your sorting should be - concise!
This function, I definitely LIKE! ;)

Your code is wonderfully playful, great creativity, but never show it to someone as something like working code you wrote. If you ever need to extend or modify it, after you've been away from it for awhile, you will see what a nightmare on Elm St. it is to work with.

Thank you for criticizing my work …

zeroliken 79 Nearly a Posting Virtuoso

Here is a complete and working code that
1. Adds the numbers(input) to get a sum(output)
2. Sorts the numbers in a descending order

Can you post any suggestions on how I can improve my code :)

main.c

#include <stdio.h>
#include "header.h"
main (){	
		int sum, x, y, z,choices;
		input(&x, &y, &z);
		printf ("\nChoose function\n");
			do{
			printf("\n1 -Input-Output (Add) function \n");
  			printf("2 - Swap and Sort function \n");
  			printf("3 - Quit\n");
			scanf ("%d", &choices);
			switch (choices) {
			case 1: addfunction(&x, &y, &z, &sum);
			        printf ("%d+%d+%d=%d.\n",x,y,z,sum);
			break;
			case 2: sortnswap(&x, &y, &z); 
			         printf ("%d %d %d \n", x, y, z);
			break;
			case 3: printf ("\nprogram terminated!!!\n");
			break;
		}
	}
	while(choices!=3);
}

header.h

void input (int *a, int *b, int *c) {                          //Input Function

	printf ("\nEnter x: ");
	scanf ("%d", a);
	printf ("Enter y: ");
	scanf ("%d", b);
	printf ("Enter z: ");
	scanf ("%d", c);
	}
	
void addfunction(int *a, int *b, int *c, int *sum) {	        //Output function
	*sum=*a+*b+*c;	
	}

void sortnswap(int *a, int *b, int *c) {                         //Sort and Swap Function
	int hi,lo,mid;	
	printf ("\nThe numbers sorted from descending order\n");	
	if (*a>=*b && *a>=*c && *b>=*c) {
		hi = *c; 
		mid = *b;
		lo = *a;
		}		
	else if (*a>=*b && *a>=*c && *c>=*b) {
		hi = *b;
		mid = *c;
		lo = *a;
		}		
	else if (*b>=*a && *b>=*c && *c>=*a) {
		hi = *a;
		mid = *c;
		lo = *b;
		}		
	else if (*b>=*a && *b>=*c && *a>=*c) {
		hi = *c;
		mid …
zeroliken 79 Nearly a Posting Virtuoso

When I compile it the squares that you said that is suppose to be in the top right quadrant appear in the bottom right quadrant

while(b<51)
{
g.setColor(new Color(r.nextInt(COLOR), (0), (0)));
g.fillRect(MIDX+50, MIDY-50, r.nextInt(48)+2, r.nextInt(48)+2);
b++;

the y value coordinate in computers has the opposite value from the regular cartesian coordinate plane meaning the upper side is negative and the lower side is positive

zeroliken 79 Nearly a Posting Virtuoso

Can you include the Stage class in your code

zeroliken 79 Nearly a Posting Virtuoso

Why are there so many worthless posts here? The answer is extremely simple:

There is no function (why would there be?)
Use = on 2 consecutive characters.


We don't give answers here. We help them fix their attempt.

As for your code: main(){ -- main is always an int scanf("%s",string); -- see this for(i=0;i<strlen(string);i++) -- why calculate the length of the string every time through the loop? That's a waste of resources. And why compare the last character with the \0 at the end? if(string[i]==string[i+1]){ -- indent properly printf("%c\n", string[i]); -- indent properly
where's your required return statement?

Sorry about that the problem seems so easy I just couldn't help myself do the code,
and yes I agree the using strlen is a waste of time but I just thought that a (any)string function a part of his requirement so I include it.

zeroliken 79 Nearly a Posting Virtuoso

Here's the answer plus it has a string function

#include<stdio.h>
#include<string.h>

main(){
	char string[20];
	int i;
	printf("Enter string:");
	scanf("%s",string);
	
    for(i=0;i<strlen(string);i++)
    {
    if(string[i]==string[i+1]){
    printf("%c\n", string[i]);
    }
    }
}
zeroliken 79 Nearly a Posting Virtuoso

Does it really need to be a String function?

Because you can use a simple for loop , if/else statement and a counter to output the consecutive character

Though if (any) String Function is required at all you can use strcmp on each index(a letter) and the index after that(next letter) to know if it is consecutive though it is inefficient.

Note that your going to find a way to get each letter according to its index and use a for loop and if else statement along with strcmp to find a consecutive letter.

zeroliken 79 Nearly a Posting Virtuoso

Does it really need to be a String function?

Because you can use a simple for loop , if/else statement and a counter to output the consecutive character

Though if (any) String Function is required at all you can use strcmp on each index(a letter) and the index after that(next letter) to know if it is consecutive though it is inefficient.

zeroliken 79 Nearly a Posting Virtuoso

Firstly, When I click on the link it requires a login to continue

Also it would be better to put some parts of the code in a new class to make it efficient and lessen the lines of code plus it can help us pinpoint what part of the code has the error Cause its really hard to find an error with a program that has 675 lines of code.

Lastly some comments on each method would be nice

You can also use this to make it more presentable and efficient

import javax.swing.*;
      import java.awt.*;
zeroliken 79 Nearly a Posting Virtuoso

can you post which part of your code do you need help or wish to add something?

and like what stevanity said, please post what the program is for, its hard for us to pinpoint exactly what part of the code needs a change or something

zeroliken 79 Nearly a Posting Virtuoso

@stultuske thanks for correcting my code anyway I forgot to remark it as incomplete and
rushed.I just wanted to show here how a part of how JOptionpane works.

Sorry about that I should have posted a more "friendly" solution rather than give branflake a new problem along the way ...my bad :p

zeroliken 79 Nearly a Posting Virtuoso

@stultuske you have a good point and I admit that my way is far from the best solution its just that I've gotten used to using a new class in my works.

anyways it was only a suggestion :)

zeroliken 79 Nearly a Posting Virtuoso

You can start by asking two inputs from the user
namely the word and letter

then use a for loop and if else statement to get the index position

example

for(i=0;i<strlen(word);i++){
				if(word[i]==letter){
					printf("The position of the letter is %d\n",i);
					break;
					}
		}

Of course there are missing pieces of this code you figure out the rest or maybe make a code of your own.

P.S. N1GHTS and WaltP are right you should post a code of your own and tell us the problem and not just ask us for an instant code

zeroliken 79 Nearly a Posting Virtuoso

Here's my Example

public double calcinsDeduct()
	{
      String cmdString =
        JOptionPane.showInputDialog("M-Male or F-Female: ");
 
        if(cmdString == ("M")){
          JOptionPane.showMessageDialog(null,"50.00 - deducted"); //shows the message...you can edit this 
	 insDeduct = 50.00;
	}
       else if(cmdString == ("F")){
          JOptionPane.showMessageDialog(null,"100.00 - deducted");
	 insDeduct = 100.00;
	}
	else{
          JOptionPane.showMessageDialog(null, "enter either M or F");
	}
	return insDeduct;  
	}

you should specify how the JOptionPane should look or work like

check here for more examples
http://http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#features

zeroliken 79 Nearly a Posting Virtuoso

One thing I notice is that you dont have a main method
Is this on purpose?

public static void main(String[] args){
//...
}
zeroliken 79 Nearly a Posting Virtuoso

all you need to do is to store the numbers in another class

help.java

import java.util.Scanner;
import java.util.*;

public class help
{

public static void main(String[] args)
{
int num;
int total=0;
int i=0;

help2[] entries=new help2[100];
int counter=0;

Scanner sc = new Scanner(System.in);

do
{
System.out.print("Input an integer. Input 0 to finalize: ");
num = sc.nextInt();

   entries[counter]=new help2(num);
   counter++;

if (i==0)
{
total = num;
}
else
{
if (i%2==0)
{
total = total + num;
}
else
{
total = total-num;
}
}
i++;
}
while (num!=0);
System.out.println("The number of input is:" +i);
   for(int j=0;j<counter-1;j++){
	if (j%2==0)
	{
		if(j!=counter-2){
			System.out.print(entries[j].getNum()+ "-");
		}	
		else{	
			System.out.print(entries[j].getNum());
		}
	}else{
		if(j!=counter-2){
			System.out.print(entries[j].getNum()+ "+");
		}	
		else{	
			System.out.print(entries[j].getNum());
		}
	}
}
System.out.println("="+total);
}
}

help2.java

public class help2{

private int num;

   public help2(int num){
	this.num = num;
   }
   public void setNum(int num){
   this.num=num; 
   }
   public int getNum(){
   return this.num;
   }
}
WaltP commented: Don't post answers to questions. It's not YOUR homework. -4
zeroliken 79 Nearly a Posting Virtuoso

Since when is not knowing how to use a function an "issue" with the function? That's more like an issue with the programmer.

Okay then let me rephrase what ive said..
..Someone should have told me "my mistake" using strtok and strcat..

zeroliken 79 Nearly a Posting Virtuoso

Thanks everyone for the replies especially to Stazloz who pointed out all the flaws of my program and gave tips for coding

Ive found that the "easiest" solution to the problem is to store the words that were tokenized and then use them for the letter function

..Someone should have told me that there is an issue with strtok and strcat..

Well anyway Thanks again

zeroliken 79 Nearly a Posting Virtuoso

The program should change a sentence to pig Latin.

Rules
A word starting with a vowel should start with a capital letter and the rest with small letters and end with "way".

If a word starts with a consonant the first letter will move to the last until it starts with a vowel then make the First letter capital and the rest small letters and finally ends the word with "ay"

Example

PLEASE hElp Me I NeEd You

output:

Easeplay Elphay Emay Iway Eednay Ouyay

Main.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"function.h"

main(){
char word[100]; 

input(word);
divider(word);
}

function.h

void input(char string[]){               //gets a string
printf("input a string:");
gets(string);
}

void letter(char string[]){              //translates word/sentence to pig latin
char cons[100];
char vows[100];
int i=0,j=0,a,x,c,k,q=0,d;

strcpy(vows,"aeiouAEIOU");
strcpy(cons,"bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ");

   
for(i=0;i<strlen(vows);i++){                 //checks IF THE WORD STARTS WITH A vowel
   if(string[0]==vows[i]){   
     string[0]=toupper(string[0]);		//changes first letter to capital
       for(k=1;k<strlen(string);k++){		//changes the rest to lower case
           string[k]=tolower(string[k]);
           }
	strcat(string,"way"); 			//then adds the word "way"
	break;					//stops the operation
          }
         }
         
for(d=0;d<strlen(cons);d++){                 //checks the consonants  
do{         
for(j=0;j<strlen(cons);j++){
      if(string[0]==cons[j]){                //if the word starts with a consonant
         
         strncat(string,string,1);           //adds the first letter to the last
         string[0]=' ';                      //removes the first letter
         a=0; 
         for(a=0;a<=strlen(string);a++){     //allocates the location of individual string        
         string[a]=string[a+1];
                }        
               
         for(x=0;x<strlen(vows);x++){        
      if(string[0]==vows[x]){                //checks if the first letter has become a vowel
         string[0]=toupper(string[0]);		//changes first case to upper
        
         for(c=1;c<strlen(string);c++){
         string[c]=tolower(string[c]);                  
                 }			 	//changes rest of the case to lower
	 strcat(string,"ay");		    //then it adds the word "ay"
	 break;				    //stops the operation
                } 
               } …