Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,883 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,309 people online right now. Registration is fast and FREE... Join Now!




output problem

 
Reply to this topicStart new topic

output problem

jayati
1 Jan, 2007 - 11:42 PM
Post #1

New D.I.C Head
*

Joined: 11 Jun, 2006
Posts: 4


My Contributions

CODE
content=// Program to input any string and print the number of spaces stored
// within the string at prime positions.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
main()
{
char a,name[50];
int f=0, l=0,c=0,k=0;
cout<<"Enter any string "<<endl;
gets(name);
l = strlen(name);
cout<<"The length of the string is "<<l;
cout<<endl;
for(int i=0;i<l;i++)
{
a=name[i];
if (a==' ')
// checking if i is prime
k=i+1;
for(int l=1;l<=k;l++)
{
if (k%l==0)
f=f+1;
}
if (f==2)
c = c + 1;
f=0;
}
cout<<endl<<"The no. of spaces at prime position is  "<<c;
getch();
return 0;

}

pirate.gif i complied the programme but couldn't get the correct answer that i was supposed to get. plz help
User is offlineProfile CardPM
+Quote Post

Manny
RE: Output Problem
2 Jan, 2007 - 12:12 AM
Post #2

New D.I.C Head
*

Joined: 30 May, 2006
Posts: 44


My Contributions
[/quote]

char a,name[50];

[quote]

Try and use some thing like this for the name:

string finstName;

string lastName;

User is offlineProfile CardPM
+Quote Post

Manny
RE: Output Problem
2 Jan, 2007 - 12:18 AM
Post #3

New D.I.C Head
*

Joined: 30 May, 2006
Posts: 44


My Contributions
QUOTE(Manny @ 2 Jan, 2007 - 01:12 AM) *



char a,name[50];

QUOTE


Try and use some thing like this for the name:

string finstName;

string lastName;


And you should change the header files to:

#include <iostream>
#include <string>

Add

#include <cstring>

User is offlineProfile CardPM
+Quote Post

horace
RE: Output Problem
2 Jan, 2007 - 12:28 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
for a start try putting {} around the section of code to be checked if a == ' ', e.g.
CODE

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
main()
{
char a,name[50];
int f=0, l=0,c=0,k=0;
cout<<"Enter any string "<<endl;
gets(name);
l = strlen(name);
cout<<"The length of the string is "<<l;
cout<<endl;
for(int i=0;i<l;i++)
{
a=name[i];
if (a==' ')
{  // ** added
// checking if i is prime
k=i+1;
for(int l=1;l<=k;l++)
{
if (k%l==0)
f=f+1;
}
if (f==2)
c = c + 1;
f=0;
}  // ** added
}
cout<<endl<<"The no. of spaces at prime position is "<<c;
getch();
return 0;

}



User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Output Problem
2 Jan, 2007 - 07:40 PM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,029



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
jayati, a lot of people commit the mistake of missing out on closing braces because they don't indent their code.

This is what your code looks like when indented.
CODE

// Program to input any string and print the number of spaces stored
// within the string at prime positions.

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

main()
{
    char a,name[50];
    int f=0, l=0,c=0,k=0;
    cout<<"Enter any string "<<endl;
    gets(name);
    l = strlen(name);
    cout<<"The length of the string is "<<l;
    cout<<endl;
    for(int i=0;i<l;i++)
    {
        a=name[i];
        if (a==' ')// checking if i is prime
            k=i+1;
      for(int l=1;l<=k;l++)
        {
            if (k%l==0)
                f=f+1;
        }
        if (f==2)
            c = c + 1;
       f=0;
    }
    cout<<endl<<"The no. of spaces at prime position is  "<<c;
    getch();
    return 0;
}

Everyone has their own indentation style, but what I really want to say is that finding out such mistakes is really easy with indented.
Not only that, indented code is more readable.

And as Manny indirectly pointed out, the iostream.h and string.h (even conio.h) are not part of the standard.

Use <iostream> , <string> and <cstring>

Don't use conio.h at all unless your target OS is Windows. functions in conio.h are not portable.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 12:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month