Post your C/C++ Programs Here

Status
Not open for further replies.

achalaxp

Right off the assembly line
Re: Post ur C/C++ Programs Here

Thanx for the example program , I also found such programmes but couldnt find the definition of "1LL"

also i couldnt compile ur example in VC++ 6 give me on that too.

---------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

int x;
printf("Enter digits to shift > ");
scanf("%d", &x);

-> printf("%Ld\n",1LL<<x);
printf("%Ld\n",1LL);

exit(0);
return(0);
}










C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(11) : error C2059: syntax error : 'bad suffix on number'
C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(11) : error C2146: syntax error : missing ')' before identifier 'L'
C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(11) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(12) : error C2059: syntax error : 'bad suffix on number'
C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(12) : error C2146: syntax error : missing ')' before identifier 'L'
C:\Program Files\Microsoft Visual Studio\MyProjects\Test\Text.c(12) : error C2059: syntax error : ')'
Error executing cl.exe.
Text.obj - 6 error(s), 0 warning(s)

-------------------------------------------------------------------------------------------

please give that also...

Thax
 
Last edited:

mehulved

18 Till I Die............
Re: Post ur C/C++ Programs Here

Thanx for the example program , I also found such programmes but couldnt find the definition of "1LL"
That's integer 1 but it's long long 1. L is for long.
also i couldnt compile ur example in VC++ 6 give me on that too.
please give that also...
I don't have VS nor windows, thus I can't do that. My program doesn't have any errors per se so if you run it on gcc it will work. Maybe someone like dheeraj_kumar or ZeeshanQ can point out why it is going wrong with VC++.
And isn't VC++ 6 really old? VS Express Edition should be available for free, try that.
 

c2tarun

N3CrO..NiNj@**
Re: Post ur C/C++ Programs Here

Did you even read my entire post? It wasn't that long.

You're allocating space for _one_ integer here :



You're then creating a pointer to it here :



But then, you're treating that pointer like an array, here :



In doing this, you are accessing memory outside of the space that you declared with "int a;". This memory outside your variable is _not_ guaranteed to remain the same between accesses.

As you are using a screwed compiler in Turbo C, it ignores this and prints whatever is there at the time, which is junk. When I compile your program with gcc, it segfaults, and with good reason.








Thanx Sykora
thanx for everything you explained me........
that really helped me a lot........
good day
 

red_devil

Back!
Re: Post ur C/C++ Programs Here

was going through one of the online resources suggested in another thread...

found this one randomly... but i didn't understand why the following was used..
Code:
cp == &in_line[LINELNG-1]
Pls tell me why that was used...[the code is highlighted in red ;) ]

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINELNG 100     /* max. length of input line */

main(){
      char in_line[LINELNG];
      char *cp;
      int c;

      cp = in_line;
      while((c = getc(stdin)) != EOF){
              if([COLOR=Red][B]cp == &in_line[LINELNG-1][/B][/COLOR] || c == '\n'){
                      /*
                       * Insert end-of-line marker
                       */
                      *cp = 0;
                      if(strcmp(in_line, "stop") == 0 )
                              exit(EXIT_SUCCESS);
                      else
                              printf("line was %d characters long\n",
                                      (int)cp-in_line);
                      cp = in_line;
              }
              else
                      *cp++ = c;
      }
      exit(EXIT_SUCCESS);
}
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

The code is getting a string from the user and storing it in an array. The code you've highlighted, is simply checking if it has reached the end of the array, and if so inserts the EOL marker. The LINELNG - 1 part is to make sure that there is one space available for the EOL marker itself.
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

Sykora, since LINELNG is 100 so LINELNG-1 is 99. in_line[99] *is* the last position in the error. There is no more space left in the array in_line. Isn't this correct?
 

aniket.awati

I am the Legend.........
Re: Post ur C/C++ Programs Here

Sykora, since LINELNG is 100 so LINELNG-1 is 99. in_line[99] *is* the last position in the error. There is no more space left in the array in_line. Isn't this correct?
In that case, it should be -2 rather than -1.

But, i think this statement is to check if pointer has reached the final position of the array, in such case, the program will write eol at that position. i.e.
Code:
*cp=0;
 
Last edited:

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

@ilugd: That's exactly what I said.

It checks if it is currently at the last position, and if so inserts an EOL at that position.

@aniket.awati: -1 *is* the last position. The EOL goes there. not at -2.
 

aniket.awati

I am the Legend.........
Re: Post ur C/C++ Programs Here

I was editing the reply, when you replied. Yeah it certainly should be -1 not -2, missed that while editing.
 

c2tarun

N3CrO..NiNj@**
Needed help with this program

// This is a Program to convert Date from American format to Normat format
// From "July 15, 1986" to "15 July 1986"

import java.util.*;

class datetrans
{
public static void main(String args[])
{
System.out.println("Enter the date in \"July 15, 1986\" format:");
Scanner stdin=new Scanner(System.in);
String date=stdin.nextLine();
String space=" ";
int buf1=date.indexOf(space,0);
String month=date.substring(0,buf1);
String comma=",";
int buf2=date.indexOf(comma,buf1+1);
String day=date.substring(buf1,buf2-buf1);
String year=date.substring(buf2+1,date.length()-buf2);

System.out.println(day+"-"+month+"-"+year);
}
}


/* This java program is not working as expected. Its giving and Index out of bounds error in line number 18, though it is not exactly occurring. Can anyone please tell me why this is happening */
 
Last edited:

c2tarun

N3CrO..NiNj@**
regret

i am sorry for my last quote..[it was posted by my friend who used my account] can u help me with that program
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Does your cat type too or do you still not get the difference between C/C++ and Java?
 

aniket.awati

I am the Legend.........
Re: Post ur C/C++ Programs Here

IMHO this sticky should be open for all langs.

As for the code, problem is in the buffers used.
I think this would solve the problem.
Code:
import java.util.*;
import javax.swing.*;
class datetrans
{
public static void main(String args[])
{
String date=JOptionPane.showInputDialog("Enter the date in \"July 15, 1986\" format:");
String space=" ";
int buf1=date.indexOf(space,0);
String month=date.substring(0,buf1);
String comma=",";
int buf2=date.indexOf(comma,buf1+1);
String day=date.substring(buf1,buf2-1);
String year=date.substring(buf2+1);

JOptionPane.showMessageDialog(null,""+day+"-"+month+"-"+year);
}
}
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Simpler:
Code:
import java.lang.*;
import java.text.*;
import java.util.*;

class date
{
	public static void main(String args[]) throws ParseException
	{
		String textDate = "July 4, 1898";
		SimpleDateFormat input = new SimpleDateFormat("MMMMMMMMM d, yyyy");
		SimpleDateFormat output = new SimpleDateFormat("d MMMMMMMMM yyyy");
		Date df = input.parse(textDate);
		String date ="Date: "+output.format(df);
		System.out.println( date );
	}
}
 
Status
Not open for further replies.
Top Bottom