12 Replies - 808 Views - Last Post: 03 December 2010 - 01:59 PM Rate Topic: -----

#1 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:05 PM

hello im trying to creat a program that will allow a user to input a sentence then choose whether to output it after and im having problems here is my code
#include <cstdlib>
#include <iostream> 
#include <string> 

using namespace std;

int main(int argc, char *argv[])
{
    int var1;
    int var2;
    string mystring = "";
    cout << "press 1 to leave a message, or press 0 to exit \n";
    cin >> var1;

    if(var1 = 1)
    {
            cout << "type your message now\n";
            getline(cin, mystring);
    }
    /*else if(var1 = 0)
    {
         goto a;
    }
     */    

    
    var1 = 3;
    cout << "press 2 to playback message\n";
    cin >> var2;
    if(var2 = 2)
    {
            cout << mystring;
            cin >> var2;
    }
//  a:  
    
    system("PAUSE");
    return 0;
}


the program runs as follows when executed
"press 1 to leave a message, or press 0 to exit"
1 (ENTER)
"type your message now"
"press 2 to playback message"
2 (ENTER) //to see if it womehow got a message recorded inadvertantly
(nothing)
2 (ENTER)
"press any key to continue..."

and this is how i want it to run

"press 1 to leave a message, or press 0 to exit"
1 (ENTER)
"type your message now"
hello world (ENTER)
"press 2 to playback message"
2 (ENTER)
"hello world"
"press any key to continue..."



where am i going wrong and how should i fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Sentence Input storage then output (Problem)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:09 PM

if(var1 = 1)

That's an ASSIGNMENT, not a COMPARISON. = for assignment, == for comparison.
Was This Post Helpful? 0
  • +
  • -

#3 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:11 PM

still same run no different
also you probably noticed the var1 = 3 before the cin var2 i was originaly using var1 in both spots...

This post has been edited by codermonk: 03 December 2010 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:12 PM

Did you fix ALL the instances where you made that mistake?
Was This Post Helpful? 0
  • +
  • -

#5 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:15 PM

yes i fixed them all... it is a short program after all lol
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:16 PM

OK, so post the current code please.
Was This Post Helpful? 0
  • +
  • -

#7 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:20 PM

#include <cstdlib>
#include <iostream> 
#include <string> 

using namespace std;

int main(int argc, char *argv[])
{
    int var1;
    int var2;
    string mystring = "";
    cout << "press 1 to leave a message, or press 0 to exit \n";
    cin >> var1;

    if(var1 == 1)
    {
            cout << "type your message now\n";
            getline(cin, mystring);
    }
    /*else if(var1 == 0)
    {
         goto a;
    }*/
       

    
    var1 = 3;
    cout << "press 2 to playback message\n";
    cin >> var2;
    if(var2 == 2)
    {
            cout << mystring;
            cin >> var2;
    }
//  a:  
    
    system("PAUSE");
    return 0;
}



when i used
 cin >> mystirng; 
it would throw an infinite loop



i juse cannot for the life of me see anywhere ive goe wrong this code should work properly in my mind.... although my mind isnt a computer dumb things...


yet another edit sorry... the string in the string initialisation isnt bold like all the int's and namespace and all that stuff does that have something to do with it?

This post has been edited by codermonk: 03 December 2010 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:45 PM

You're not using cin >> mystring;. You are better off using cin.getline().

The current problem is that the newline is left in the buffer following cin >> var1; and cin >> var2;. Add a cin.ignore() following each of these to remove the newline.
Was This Post Helpful? 0
  • +
  • -

#9 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:48 PM

okay now it outputs the message but it doesnt go directly to press a key... which it should right?
#include <cstdlib>
#include <iostream> 
#include <string> 

using namespace std;

int main(int argc, char *argv[])
{
    int var1;
    int var2;
    string mystring = "";
    cout << "press 1 to leave a message, or press 0 to exit \n";
    cin >> var1;
    cin.ignore();
    if(var1 == 1)
    {
            cout << "type your message now\n";
            getline(cin, mystring);
    }
    /*else if(var1 == 0)
    {
         goto a;
    }*/
       

    
    var1 = 3;
    cout << "press 2 to playback message\n";
    cin >> var2;
    cin.ignore();
    if(var2 == 2)
    {
            cout << mystring;
            cin >> var2;
    }
//  a:  
    
    system("PAUSE");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:51 PM

Well it would, except that you're waiting for the user to enter an integer right after you output the string.
Was This Post Helpful? 0
  • +
  • -

#11 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:53 PM

whoops....
didnt see that forgot about it... thank you very much for the help the program runs as wanted now anyway i can give you kudos or something for the excellent and rapid help?
Was This Post Helpful? 0
  • +
  • -

#12 TomJoad  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 54
  • Joined: 01-December 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:57 PM

I would break it up a little, and use switch statements instead of if statements.
Was This Post Helpful? 0
  • +
  • -

#13 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Sentence Input storage then output (Problem)

Posted 03 December 2010 - 01:59 PM

yeah i would do that if it were really going to be anything more than it is. the programs finished now completely and works excellent. and for the purpose i wrote it for it doesnt really need to be more efficient.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1