[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

I need help with my c++ program/please

Hello I take a C++ college course and my program was due last week but I want to still finish it.
It is supposed to count the number of words a person inputs with the functions. I used num_word for the function and my_alpha and have loops and everything but the output screen just lets them input words and then it says the number of words read is 0. It always says zero. I need help, I don't want to fail and lose 4 credit hours and Im a senior in H.S.
0
djabstract
Asked:
djabstract
  • 3
  • 3
  • 3
  • +4
1 Solution
 
djabstractAuthor Commented:
I pray someone can get back at me by monday. This is too hard for me.
thanks.
0
 
ozoCommented:
How are you counting the words?
0
 
Lab_RatCommented:
Another 1 liner ozo?
I likes thems :)

Oooops... notify :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
KangaRooCommented:
Bad spot to place your C++ question, there is a C++ section. Anyway, it might be a good ide if you show us some of your code.
Maybe this could be done in one line...
0
 
Lab_RatCommented:
I think I remember that it has, with standard library nested function calls.
0
 
mflamCommented:
Send the code, I'll fix it. (Todays Monday, but in Israel I'm before ya...)
0
 
KangaRooCommented:
What the (Edited by Computer101) are you doing mflam?
0
 
azamiCommented:
Hard to say without looking at code, but:

If you are passing a variable for your count into a function that is supposed to increment it, you may be passing "by value".  In this case, the function is working with a copy of the variable, not the variable itself:

void f(int i)
{
    i++;    //this is just a copy
}

void main()
{
    int a = 0;
    cout << a << endl;
    f(a);
    cout << a << endl;    //still 0!
}

The above program makes a copy of a and passes it to f(), which then increments the copy and does not change a at all.

Pass a pointer or reference to the function instead.

(I know this is way late for this assignment, but I hope that djabstract is interested enough in software development to want the lesson anyway.)

If this is not the problem, I'd be glad to take a look at the code and make suggestions.
-Matthew
0
 
Lab_RatCommented:
void flumps(int &i)
{
    i++;    //this is the ORIGINAL
//               Sneaky Eh?;)
}

void main(void)
{
    int a = 0;
    cout << a << endl;
    flumps(a);
    cout << a << endl;    //Not 0!!!!
}

0
 
mflamCommented:
Sorry, I was new to forum.
Figured, just like myself, if I have a fixed program, I look at it, and understand it...

Anyways, it's left open...
I suppose for djabstract it's too late, but:
IMHO what he's looking for is a program that reads in words, and counts them.
The errors he/she has are probably:
1. Initializing the arrays.
2. Using vars instead of pointers.
3. Typo's, using the wrong variable name.
4. Problems with output.
5. Problems with strings.

We gotta see the code to find out where the errors are, and point them out.

In Pseudo code the answer would be:
// Variables: StringArray, count
// Functions: CountWords(StringArray)
// Initialize all variables
// Input from user
//   eg. cin >> StringArray
// count = CountWords(StringArray)
//  NOTE: use pointer
//  eg. if was defined stringArray[40]
//      use CountWords(stringArray)
/       equivalent to &stringArray[0]
// cout << count << endl;



0
 
KangaRooCommented:
No problem.
Agreed, djabstract we need to see your code before we can try to find the errors that may be in it.
0
 
sureshkumarCommented:
hi

Your problem counting the no. of words in the given string.Is it right?????
0
 
sureshkumarCommented:
hi

Your problem counting the no. of words in the given string.Is it right?????
0
 
sureshkumarCommented:
if that is your problem i am giving solution for that have a look.

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

int main()
{

char a[100];
int word=1;count,i;

cout<<"enter the string to terminate !";
cin.get();
cin.getline(a,1000,'!');

for(i=0;i<=1000;i++)
{
if(a[i]=='!')
{
break;
}
count=i;
}

for(i=0;i<=count;i++)
{
if(a[i]==' ')
{
word++;
}
}
cout<<"no of words is "<<word;
return 0;
}

for the further queries welcome
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now