Thread for the C/C++ Beginners and Programmers!

#include <iostream>
using namespace std

[


int x;
cout << "Enter number between 1 to 7 : ;
cin >> x;

if (x<1)
cout << "Try Again";

else if (x>7)
cout << "Try Again";

else
cout << "The number you entered is " >> x >> "\n";

return 0;

]

I hope its right! :)
 
Last edited:
To accept no from 1-7

#include<stdio.h>
main()
{

int x;

printf("Enter the number (1-7)\n");
while(1)
{
if((x>=1) && (x<=7))
{
scanf("%d",&x);
}
else
{
printf("Try again..Invalid range\n");
}
}
}
 
That's right Surendar. Reps for you.

That is one of the many ways in which this program could be done. Another interesting way is through the use of recursive functions -- anybody care to try out the question using that approach ?

Viral, your method is not wholly right in the sense that the user will get the "invalid range" message only once ... not as many times as required in the question.
 
void input () //function to accept the entry
{
int x;
cout<<"Enter the no. (1-7) : ";
cin>>x;
if (x<0 || x>7)
{
cout<<"Invalid Entry, Please enter again !!!";
input(); //calling the function again if the input is wrong
}
}
 
ritwik said:
Viral, your method is not wholly right in the sense that the user will get the "invalid range" message only once ... not as many times as required in the question.

Yes, I forgot the loop altogether! :p
 
void input () //function to accept the entry
{
int x;
cout<<"Enter the no. (1-7) : ";
cin>>x;
if (x<0 || x>7)
{
cout<<"Invalid Entry, Please enter again !!!";
input(); //calling the function again if the input is wrong
}
}

Yes, that is correct abhas. Good work. Glad to see you've (finally) been cured of the goto habit ...

So, anybody got any more C++ questions ?
 
Bingo.

That was easy,as the d was replaced by c so it became %c.So the ascii value was printed.Good work.
 
ritwik said:
Yes, that is correct abhas. Good work. Glad to see you've (finally) been cured of the goto habit ...

So, anybody got any more C++ questions ?
lol, ya, but i got rid of goto long ago (started using infinite while loop coupled with exit(), and continue instead of break (in the switch statements))
:p
 
i m reviving this thread.... :happy


Find the output for a simple C program :


main()
{
int i=-5,j=-2;
junk(i,&j);
printf("i= %d, j=%d",i,j);
}

junk(i,j)
int i,*j
{

i = i*i;
*j=*j * *j;

}

Assume address of i and j be 2000 and 3000.
:cheers
 
Oh well, I will awaken this thread with a intresting file handling question.
I have attached a txt file which contains name of movies and their timings for the current month which will be shown on HBO

this file contains content file this:

01:35 AM Fallen
<time> <movie>

Your first job is to extract all movies into a txt file.

then u have to remove the repeat shows.Like, HBO's Big premire, and repeat movies.
then save it into another file.

post those files and the codes to get reps! ;)
let's see who has the best algorithm. :)
 
That would be a somewhat long program, it is pesky but not very difficult if you think it over. Did this for my C++ project. Anyways, file handling is a somewhat boring aspect of CPP.

EDIT: Does anybody here have some idea about Win32 programming using VC++? If yes, I'd really appreciate a little primer on it, since it will help me in learning OpenGL ;)
 
Ritwik said:
That would be a somewhat long program, it is pesky but not very difficult if you think it over. Did this for my C++ project. Anyways, file handling is a somewhat boring aspect of CPP.

not long, it would need a good algorithm.

Ritwik said:
EDIT: Does anybody here have some idea about Win32 programming using VC++? If yes, I'd really appreciate a little primer on it, since it will help me in learning OpenGL ;)
I might have.let me check...
 

Users who are viewing this thread

Top